C++编写的WebSocket服务端客户端实现示例代码

目录

使用过标准的libwebsockets服务端库测试过,主要是短小精悍,相对于libwebsockets不需要依赖zlib和openssl 以及其他库,直接make就可以使用了,linux跟windows都可以使用。

测试用例:

#include "easywsclient.hpp"
#include <assert.h>
#include <stdio.h>
#include <string>

using easywsclient::WebSocket;
static WebSocket::pointer ws = NULL;

void handle_message(const std::string & message)
{
    printf(">>> %s\n", message.c_str());
    if (message == "world") { ws->close(); }
}

int main()
{
    ws = WebSocket::from_url("ws://localhost:8126/foo");
    assert(ws);//判断ws对象是否为空null
    ws->send("goodbye");
    ws->send("hello");
    //如果你需要多线程,可以在一个thread 维护该ws的连接重连机制
    while (ws->getReadyState() != WebSocket::CLOSED) //判断ws是否正常连接
    {
      ws->poll();//这个必须要调用,否则不能发送,发送跟接收都是异步的,都是在这个poll函数里监测处理的
      ws->dispatch(handle_message);
    }
    delete ws;
    return 0;
}
//线程thread  维护重连连接
void run()
{
    bool conn = FLASE;
    ws = WebSocket::from_url("ws://localhost:8126/foo");

    //如果你需要多线程,可以在一个thread 维护该ws的连接重连机制
    while (1) //判断ws是否正常连接
    {
      if(ws != NULL)
      {
          ws->poll(0);//这个必须要调用,否则不能发送,发送跟接收都是异步的,都是在这个poll函数里监测处    理的
          ws->dispatch(handle_message);
          if(ws->getReadyState() == WebSocket::CLOSED)
          {
             //ws连接断开 重连
             delete ws;
             ws = NULL;
             ws = WebSocket::from_url("ws://localhost:8126/foo");
          }
          else if(wss->getReadyState()== WebSocket::OPEN)
          {
             //ws连接ok
             //    ws->send("goodbye");
             ws->send("hello");
          }

      }
      else
      {
        ws = WebSocket::from_url("ws://localhost:8126/foo");
        sleep(1);
      }
      usleep(100);
    }
    if(ws!=NULL)
    delete ws;
}

有细心的朋友发现在发送中文GBK 的时候与服务端会断开

//GBK -> UTF-8
//遇到发送的字符串里有中文的话需要send 前进行转换一下,
//这个是网友提供的在windows下的转换函数
std::string Server_Stream::GBKToUTF8(const std::string& strGBK)
{
 std::string strOutUTF8 = "";
 WCHAR * str1;
 int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);
 str1 = new WCHAR[n];
 MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);
 n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);
 char * str2 = new char[n];
 WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);
 strOutUTF8 = str2;
 delete[]str1;
 str1 = NULL;
 delete[]str2;
 str2 = NULL;
 return strOutUTF8;
}

下面是C++实现的WebSocket客户端,写好后这里记一下,免得以后忘记。

本示例共有三个文件组成,依赖Websocket++第三方库

其中main.cpp是使用示例

#include <iostream>
#include <string>
#include <sstream>

#include "websocket_endpoint.h"

int main(int argc, char **argv)
{
 bool done = false;
 std::string input;
 kagula::websocket_endpoint endpoint;

 endpoint.connect("ws://localhost:9002");

 while (!done) {
  std::cout << "Enter Command: ";
  std::getline(std::cin, input);

  if (input == "quit") {
   done = true;
  }
  else if (input.substr(0, 4) == "send") {
   std::stringstream ss(input);

   std::string cmd;
   std::string message;

   ss >> cmd;
   std::getline(ss, message);

   endpoint.send(message);
  }
  else if (input.substr(0, 4) == "show") {
   endpoint.show();
  }
  else {
   std::cout << "> Unrecognized Command" << std::endl;
  }
 }

 endpoint.close();

 return 0;
}

其它两个文件是封装

websocket_endpoint.h文件清单

#ifndef _WEBSOCKET_ENDPOINT_
#define _WEBSOCKET_ENDPOINT_

#include <string>

/*
Title: Web Socket Client
Author: kagula
Date: 2016-11-21
Dependencies: Websocket++、Boost::ASIO
Test Environment: VS2013 Update5, WebSocket++ 0.70, Boost 1.61
Description:
[1]Support connect a web socket server.
[2]If server is crash, client will not follow crash.
*/

namespace kagula
{
 class websocket_endpoint {
 public:
  websocket_endpoint();
  ~websocket_endpoint();

  int connect(std::string const & uri);
  void close();

  void send(std::string message);
  void show();
 };
}

#endif

websocket_endpoint.cpp文件清单

#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>

#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>

#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <sstream>

#include "websocket_endpoint.h"

typedef websocketpp::client<websocketpp::config::asio_client> ws_client;

namespace kagula
{

 class connection_metadata {
 public:
  typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;

  connection_metadata(websocketpp::connection_hdl hdl, std::string uri)
   : m_hdl(hdl)
   , m_status("Connecting")
   , m_uri(uri)
   , m_server("N/A")
  {}

  void on_open(ws_client *client, websocketpp::connection_hdl hdl) {
   m_status = "Open";

   ws_client::connection_ptr con = client->get_con_from_hdl(hdl);
   m_server = con->get_response_header("Server");
  }

  // if connection failed, the function will be invoke.
  void on_fail(ws_client *client, websocketpp::connection_hdl hdl) {
   m_status = "Failed";

   ws_client::connection_ptr con = client->get_con_from_hdl(hdl);
   m_server = con->get_response_header("Server");
   m_error_reason = con->get_ec().message();
  }

  void on_close(ws_client *client, websocketpp::connection_hdl hdl) {
   m_status = "Closed";
   ws_client::connection_ptr con = client->get_con_from_hdl(hdl);
   std::stringstream s;
   s << "close code: " << con->get_remote_close_code() << " ("
    << websocketpp::close::status::get_string(con->get_remote_close_code())
    << "), close reason: " << con->get_remote_close_reason();
   m_error_reason = s.str();
  }

  void on_message(websocketpp::connection_hdl, ws_client::message_ptr msg) {
   if (msg->get_opcode() == websocketpp::frame::opcode::text) {
    m_messages.push_back("<< " + msg->get_payload());
   }
   else {
    m_messages.push_back("<< " + websocketpp::utility::to_hex(msg->get_payload()));
   }
  }

  websocketpp::connection_hdl get_hdl() const {
   return m_hdl;
  }

  std::string get_status() const {
   return m_status;
  }

  std::string get_uri() const {
   return m_uri;
  }

  void record_sent_message(std::string message) {
   m_messages.push_back(">> " + message);
  }

  friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
 private:
  websocketpp::connection_hdl m_hdl;
  std::string m_status;
  std::string m_uri;
  std::string m_server;
  std::string m_error_reason;
  std::vector<std::string> m_messages;
 };

 std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
  out << "> URI: " << data.m_uri << "\n"
   << "> Status: " << data.m_status << "\n"
   << "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
   << "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason) << "\n";
  out << "> Messages Processed: (" << data.m_messages.size() << ") \n";

  std::vector<std::string>::const_iterator it;
  for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) {
   out << *it << "\n";
  }

  return out;
 }

 ws_client g_wsEndPoint;
 connection_metadata::ptr g_wsClientConnection;

 websocketpp::lib::shared_ptr<websocketpp::lib::thread> g_threadWS;

 websocket_endpoint::websocket_endpoint(){
   g_wsEndPoint.clear_access_channels(websocketpp::log::alevel::all);
   g_wsEndPoint.clear_error_channels(websocketpp::log::elevel::all);

   g_wsEndPoint.init_asio();
   g_wsEndPoint.start_perpetual();

   g_threadWS = websocketpp::lib::make_shared<websocketpp::lib::thread>(&ws_client::run, &g_wsEndPoint);
 }

 websocket_endpoint::~websocket_endpoint() {
  g_wsEndPoint.stop_perpetual();

  if (g_wsClientConnection->get_status() == "Open") {
   // Only close open connections
   websocketpp::lib::error_code ec;
   g_wsEndPoint.close(g_wsClientConnection->get_hdl(), websocketpp::close::status::going_away, "", ec);
   if (ec) {
    std::cout << "> Error closing ws connection " << g_wsClientConnection->get_uri() << " :" << ec.message() << std::endl;
   }
  }

  g_threadWS->join();
 }

 int websocket_endpoint::connect(std::string const & uri) {
  websocketpp::lib::error_code ec;

  ws_client::connection_ptr pConnection = g_wsEndPoint.get_connection(uri, ec);

  if (ec) {
   std::cout << "> Connect initialization error: " << ec.message() << std::endl;
   return -1;
  }

  g_wsClientConnection = websocketpp::lib::make_shared<connection_metadata>(pConnection->get_handle(), uri);

  pConnection->set_open_handler(websocketpp::lib::bind(
   &connection_metadata::on_open,
   g_wsClientConnection,
   &g_wsEndPoint,
   websocketpp::lib::placeholders::_1
   ));
  pConnection->set_fail_handler(websocketpp::lib::bind(
   &connection_metadata::on_fail,
   g_wsClientConnection,
   &g_wsEndPoint,
   websocketpp::lib::placeholders::_1
   ));
  pConnection->set_close_handler(websocketpp::lib::bind(
   &connection_metadata::on_close,
   g_wsClientConnection,
   &g_wsEndPoint,
   websocketpp::lib::placeholders::_1
   ));
  pConnection->set_message_handler(websocketpp::lib::bind(
   &connection_metadata::on_message,
   g_wsClientConnection,
   websocketpp::lib::placeholders::_1,
   websocketpp::lib::placeholders::_2
   ));

  g_wsEndPoint.connect(pConnection);

  return 0;
 }

 void close(websocketpp::close::status::value code, std::string reason) {
  websocketpp::lib::error_code ec;

  g_wsEndPoint.close(g_wsClientConnection->get_hdl(), code, reason, ec);
  if (ec) {
   std::cout << "> Error initiating close: " << ec.message() << std::endl;
  }
 }

 void websocket_endpoint::close()
 {
  if (g_wsClientConnection->get_status()=="Open")
  {
   int close_code = websocketpp::close::status::normal;
   kagula::close(close_code, "");
  }
 } 

 void websocket_endpoint::send(std::string message) {
  websocketpp::lib::error_code ec;

  g_wsEndPoint.send(g_wsClientConnection->get_hdl(), message, websocketpp::frame::opcode::text, ec);
  if (ec) {
   std::cout << "> Error sending message: " << ec.message() << std::endl;
   return;
  }

  g_wsClientConnection->record_sent_message(message);
 } 

 void websocket_endpoint::show()
 {
  std::cout << * g_wsClientConnection << std::endl;
 }
}

到此这篇关于C++编写的WebSocket客户端实现示例代码的文章就介绍到这了,更多相关C++ WebSocket客户端内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • python实现websocket的客户端压力测试

    使用python进行websocket的客户端压力测试,这个代码是从github上 找到.然后简单修改了下.大神运用了进程池,以及线程池的内容.所以保存下来,学习学习 然后需要说明的是:本次用的python2.7,也尝试用python3.6,但是老实出现websocket-client包和python3不能兼容的情况,提示没有相关的方法.所以不得已最后又采用了python2 # -*- coding:utf-8 -*- # __author__ == 'chenmingle' import we

  • C#实现WebSocket协议客户端和服务器websocket sharp组件实例解析

    看到这篇文章的题目,估计很多人都会问,这个组件是不是有些显的无聊了,说到web通信,很多人都会想到ASP.NET SignalR,或者Nodejs等等,实现web的网络实时通讯.有关于web实时通信的相关概念问题,在这里就不再做具体的介绍了,有兴趣的可以自行百度. 下面我们介绍一款WebSocket组件websocket-sharp的相关内容. 一.websocket-sharp组件概述 websocket-sharp是一个C#实现websocket协议客户端和服务端,websocket-sha

  • node.js中ws模块创建服务端和客户端,网页WebSocket客户端

    首先下载websocket模块,命令行输入 npm install ws 1.node.js中ws模块创建服务端 // 加载node上websocket模块 ws; var ws = require("ws"); // 启动基于websocket的服务器,监听我们的客户端接入进来. var server = new ws.Server({ host: "127.0.0.1", port: 6080, }); // 监听接入进来的客户端事件 function webs

  • java WebSocket客户端断线重连的实现方法

    目录 前言 Maven依赖 代码 前言 在工作中是否会遇到实用websocket客户端连接服务端的时候,网络波动,服务端断连的情况.会导致客户端被动断开连接.为了解决这个问题,需要对被动断开连接的情况进行捕获,并重新创建连接.这篇文章主要是提供可以直接使用的断线重连websocket客户端代码. Maven依赖 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</a

  • C++编写的WebSocket服务端客户端实现示例代码

    目录 使用过标准的libwebsockets服务端库测试过,主要是短小精悍,相对于libwebsockets不需要依赖zlib和openssl 以及其他库,直接make就可以使用了,linux跟windows都可以使用. 测试用例: #include "easywsclient.hpp" #include <assert.h> #include <stdio.h> #include <string> using easywsclient::WebSo

  • vue ssr+koa2构建服务端渲染的示例代码

    之前做了活动投放页面在百度.360等渠道投放,采用 koa2 + 模版引擎的方式.发现几个问题 相较于框架开发页面效率较低,维护性差 兼容性问题,在页面中添加埋点后发现有些用户的数据拿不到,排查后发现通过各个渠道过来的用户的设备中仍然包含大量低版本的浏览器. 服务端渲染 服务端渲染和单页面渲染区别 查看下面两张图,可以看到如果是服务端渲染,那么在浏览器中拿到的直接是完整的 html 结构.而单页面是一些 script 标签引入的js文件,最终将虚拟dom去挂在到 #app 容器上. @vue/c

  • ThinkPHP整合datatables实现服务端分页的示例代码

    最近做东西有一个需求,因为数据量很大,在这里我决定使用datatables的服务端分页,同时还需要传递查询条件到服务端.在网上搜索的大部分文章都感觉有些误差,于是自己封装了一下,主要配置/工具为: 服务端:php(使用thinkphp) 页面样式来自于H-ui框架(datatables版本为1.10.0) 主要修改(databases)配置项为: 1) bProcessing:true 使用ajax源 2) serverSide:true 使用服务端分页 3) createdRow:functi

  • node.js ws模块搭建websocket服务端的方法示例

    首先下载websocket模块,命令行输入 npm install ws node.js的 模块ws,可用于创建websocket服务,基本的express 和 http模块的使用 var express = require('express'); var http = require('http'); var WebSocket = require('ws'); var app = express(); var server = http.createServer(app); var wss

  • Python Websocket服务端通信的使用示例

    1.唠唠叨叨 最近又回顾了下Websocket,发现已经忘的七七八八了.于是用js写了客户端,用python写了服务端,来复习一下这方面的知识. WebSocket 是一种标准协议,用于在客户端和服务端之间进行双向数据传输.但它跟 HTTP 没什么关系,它是基于 TCP 的一种独立实现. 以前客户端想知道服务端的处理进度,要不停地使用 Ajax 进行轮询,让浏览器隔个几秒就向服务器发一次请求,这对服务器压力较大.另外一种轮询就是采用 long poll 的方式,这就跟打电话差不多,没收到消息就一

  • C# 实现WebSocket服务端教程

    .net4.5中实现了对websocket的支持 在这里我使用的是.net4.0.因此需要对原本的socket发送的数据根据websocket的协议进行解析和打包. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Sockets; using System.Threading; using System.Net; namespace We

  • java WebSocket 服务端实现代码

    1.什么是WebSocket WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端. 2.实现原理 在实现websocket连线过程中,需要通过浏览器发出websocket连线请求,然后服务器发出回应,这个过程通常称为“握手” .在 WebSocket API,浏览器和服务器只需要做一个握手的动作,然后,浏览器和服务器之间就形成了一条快速通道.两者之间就直接可以数据互相传送. 3.优点 在以前的消息推送

  • Java实现断点下载服务端与客户端的示例代码

    目录 原理 扩展-大文件快速下载思路 代码 服务端 客户端 最近在研究断点下载(下载续传)的功能,此功能需要服务端和客户端进行对接编写,本篇也是记录一下关于贴上关于实现服务端(Spring Boot)与客户端(Android)是如何实现下载续传功能 断点下载功能(下载续传)解释: 客户端由于突然性网络中断等原因,导致的下载失败,这个时候重新下载,可以继续从上次的地方进行下载,而不是重新下载 原理 首先,我们先说明了断点续传的功能,实际上的原理比较简单 客户端和服务端规定好一个规则,客户端传递一个

  • Java网络编程之简单的服务端客户端应用实例

    本文实例讲述了Java网络编程之简单的服务端客户端应用.分享给大家供大家参考.具体如下: 在Java中,我们使用java.net.Socket及其相关类来完成有关网络的相关功能.Socket类非常简单易用,因为Java技术隐藏了建立网络连接和通过连接发送数据的复杂过程.下面所说的内容只适用于TCP协议. 一.连接到服务器 我们可以使用Socket类的构造函数来打开一个套接字,如 Socket sk = new Socket("210.0.235.14",13); 其中,210.0.23

  • IDEA利用自带Axis工具和wsdl文件反向生成服务端客户端代码详细流程

    推荐教程 idea2021以下最新安装j ihuo 教程 https://www.jb51.net/article/184631.htm https://www.jb51.net/article/178193.htm 正文 IDEA,我使用的是最新版本(2021.1.1) JDK1.8 tomcat-8 详细步骤,用图片代替,关键地方,字幕解释. 到此步骤,是关键重点,请认真看 鼠标右键选择项目(soap-demo)然后请看图片 当改完之后,项目结构,会发生改变 创建-服务端-生成代码 1.请提

随机推荐