本次编译的 WebSocketpp 的vs08版本,这个说法其实不准确。
确切的说法是:因为开发用的是vs08,所以WebSocketpp需要的一些其他库,也必须是vs08版本。
【一】准备好原材料:
1、下载 websocketpp 库:https://github.com/zaphoyd/websocketpp/tree/0.7.0
2、下载 OpenSSL 库:https://www.npcglib.org/~stathis/blog/precompiled-openssl/
3、下载boost库,具体编译教程:http://blog.csdn.net/hellokandy/article/details/51149505
这里下载的OpenSSL是已经编译好的库,下载对应的版本即可。
boost库以及OpenSSL库,都放置在文件夹(D:\3rdparty),D:\3rdparty\boost_1_60_0,D:\3rdparty\openssl-1.0.2f
【二】websocketpp 的准备工作:
websocketpp下载后解压,会发现 CMakeLists.txt 文件,那就用CMake编译。
不过还需要对 CMakeLists.txt 稍作修改(设置boost库的文件路径):找到“BOOST_ROOT”,添加对BOOST_ROOT的定义:set (BOOST_ROOT "D:\\3rdparty\\boost_1_60_0")。
注意:如果之前有对BOOST_ROOT定义了环境变量,就无需对 CMakeLists.txt 进行修改了!如下图所示:
【三】CMake编译大法:
1、设置好websocketpp的源文件路径,以及cmake生成的文件存放的路径,然后点击“Configure”,在弹出来的设置对话框中,选择“Visual Studio 9 2008”,然后“Finish”。
2、接下来,勾选“BUILD_EXAMPLES”,点击“Generate”,等待编译完成!
3、编译完成后的界面,注意红色框框,表示OpenSSL的路径未设置好。不过,可以先不用管!
4、在文件夹(websocketpp-0.7.0)下,会生成build文件夹,里面就是用CMake生成的vs工程文件了!
【四】CMake编译出现的提示错误
解决办法:
找到vs08的安装路径,我的是:D:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE
把该路径添加到系统的环境变量,注意前面记得加分号!
【五】、编译echo_client、echo_server
对echo_client.cpp文件稍微做了修改,修改后的代码如下:
#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);//注释掉原来的这句代码
//收到服务器发送过来的消息后,延时两秒,再给服务器发消息
_sleep(2000);
static int count = 0;
std::stringstream val;
val.str("");
val << "count is " << count++;
c->send(hdl, val.str(), msg->get_opcode(), ec);
if (ec) {
std::cout << "Echo failed because: " << ec.message() << std::endl;
}
}
void on_open(client* c, websocketpp::connection_hdl hdl)
{
std::cout << "on_message called with hdl: " << hdl.lock().get() << std::endl;
// 给服务器发送消息 [4/28/2017 Kandy]
websocketpp::lib::error_code ec;
c->send(hdl, "Hello, this is test by kandy...", websocketpp::frame::opcode::text, ec);
}
int main(int argc, char* argv[]) {
// Create a client endpoint
client c;
std::string uri = "ws://localhost:9002";
if (argc == 2) {
uri = argv[1];
}
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));
// Add,下面这行代码目的在于触发服务器响应 [4/28/2017 Kandy]
c.set_open_handler(bind(&on_open, &c, ::_1));
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;
}
}
编译 echo_client、 echo_server工程,运行echo_clientd.exe 和 echo_serverd.exe,看看效果吧!
对WebSocket的初次体验,就到此结束了!有时间再继续学习。。。