在VS2015下配置websocket++,并用C++搭建一个简单的客户端
2016-12-20 13:34
806 查看
简介
最近在做一个项目,需要在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 <websocketpp/config/asio_no_tls_client.hpp> #include <websocketpp/client.hpp> #include <iostream> typedef websocketpp::client<websocketpp::config::asio_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 << "on_message called with hdl: " << hdl.lock().get() << " and message: " << msg->get_payload() << std::endl; websocketpp::lib::error_code ec; c->send(hdl, msg->get_payload(), msg->get_opcode(), ec); if (ec) { std::cout << "Echo failed because: " << ec.message() << std::endl; } } int main(int argc, char* argv[]) { // Create a client endpoint client c; std::string uri = "ws://这里附上你自己的ws地址"; try { // Set logging to be pretty verbose (everything except message payloads) c.set_access_channels(websocketpp::log::alevel::all); c.clear_access_channels(websocketpp::log::alevel::frame_payload); // Initialize ASIO c.init_asio(); // Register our message handler c.set_message_handler(bind(&on_message,&c,::_1,::_2)); websocketpp::lib::error_code ec; client::connection_ptr con = c.get_connection(uri, ec); if (ec) { std::cout << "could not create connection because: " << ec.message() << std::endl; return 0; } // Note that connect here only requests a connection. No network messages are // exchanged until the event loop starts running in the next line. c.connect(con); // Start the ASIO io_service run loop // this will cause a single connection to be made to the server. c.run() // will exit when this connection is closed. c.run(); } catch (websocketpp::exception const & e) { std::cout << e.what() << std::endl; } }
至此,我们就完成了websocket++的基本配置。
PS.如果编译器提示你 “错误:后面有::的名称一定是类名或命名空间名”或者 其他一些奇奇怪怪的错误,那多半是websocket++和boost的附加目录没有配置好
相关文章推荐
- 在VS2015下配置libvlc并实现一个简单RTSP的播放器
- 在VS2015下配置libvlc并实现一个简单RTSP的播放器
- 【WEB开发技术】一个简单的WEB项目验证码校验(如何从前端到后台一步一步的搭建、配置、发布整个Web项目?)
- C++ 简单的 Tcp 实现[socket] 客户端与客户端通信
- C++ 简单的 Tcp 实现[socket] 客户端与客户端通信
- C++ 简单的 Tcp 实现[socket] 客户端与客户端通信
- apache+php搭建一个简单的web环境【php调用脚本】
- 简单用C#编写socket作为客户端与Linux下C++编写服务器端通信(未完善)
- 搭建简单web项目: spring3.0基础配置(1)
- 随手写了一个简单的socket服务端和客户端
- Mondrian用于web项目的一个简单搭建实验
- 一个简单的socket服务端和客户端程序:客户端发送数据给服务端,服务端接收到数据后再给客户端发送数据
- ROS的学习(十六)用C++写一个简单的服务器(service)和客户端(client)
- 基于Linux搭建一个类似Qik手机录像直播平台(服务器端:feng streaming server + web server,客户端:Android手机应用)
- [C++] Windows下的socket编程(这是一个简单的TCP/IP例子)
- 一个简单的socket客户端发送消息到服务器端程序
- VC写的socket程序实现一个简单Echo服务器端和客户端程序
- 【简单Web服务器搭建】基于Socket实现的最简单的Web服务器【ASP.NET原理分析】
- 【简单Web服务器搭建】基于Socket实现的最简单的Web服务器【ASP.NET原理分析】
- 【转】 C++ 简单的 Tcp 实现[socket] 客户端与客户端通信