简介 最近在做一个项目,需要在C++中通过WebSocket和服务器进行通信,但我们在C++中并不能直接使用WebSocket,于是上网搜索后发现websocket++这个库很合适。 Websocket是基于HTTP协议的,或者说借用了HTTP的协议来完成一部分握手,并且它很好的支持了长连接,相较于ajax轮询和long poll更加节省资源。 具体关于WebSocket的相关知识,可以参考知乎上这篇文章: https://www.zhihu.com/question/20215561 准备工作 去GitHub上下载websocket++ https://github.com/zaphoyd/websocketpp
搭建好Boost,直接到以下网址下载编译好的,省去自己编译了(选择自己对应的ms版本,如VS2015 64位的选择boost_1_61_0-msvc-14.0-64.exe) https://sourceforge.NET/projects/boost/files/boost-binaries/1.61.0/
配置方法 第一步,新建一个工程 将项目属性调整为Release x64模式,如下图所示
第二步,打开 项目—项目属性 窗口,选择VC++目录 在“包含目录”下添加boost和websocket++的目录,如下图所示
第三步,在“库目录”下添加boost的lib包目录
点击“应用”,然后“确定”,OK大功告成。 一个简单的WebSocket客户端 我们到下载的websocket++文件夹目录下的websocketpp-master\examples\echo_client中的C++代码文件拷贝出来,在工程里测试运行即可,顺便附上代码: #include #include
#include
typedef websocketpp::client client;
using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_2; using websocketpp::lib::bind;
// pull out the type of messages sent by our config typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
// This message handler will be invoked once for each incoming message. It // prints the message and then sends a copy of the message back to the server. void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) { std::cout
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?