資源簡介
一個用C++編寫的websocket例程,代碼嚴謹,簡單易用,STL標準,同時擁有服務器端和客戶端測試

代碼片段和文件信息
#include?“client_wss.hpp“
#include?“server_wss.hpp“
using?namespace?std;
using?WssServer?=?SimpleWeb::SocketServer;
using?WssClient?=?SimpleWeb::SocketClient;
int?main()?{
??//?WebSocket?Secure?(WSS)-server?at?port?8080?using?1?thread
??WssServer?server(“server.crt“?“server.key“);
??server.config.port?=?8080;
??//?Example?1:?echo?WebSocket?Secure?endpoint
??//?Added?debug?messages?for?example?use?of?the?callbacks
??//?Test?with?the?following?javascript:
??//???var?wss=new?WebSocket(“wss://localhost:8080/echo“);
??//???wss.onmessage=function(evt){console.log(evt.data);};
??//???wss.send(“test“);
??auto?&echo?=?server.endpoint[“^/echo/?$“];
??echo.on_message?=?[](shared_ptr?connection?shared_ptr?message)?{
????auto?message_str?=?message->string();
????cout?<“Server:?Message?received:?\““?<
????cout?<“Server:?Sending?message?\““?<
????auto?send_stream?=?make_shared();
????*send_stream?<????//?connection->send?is?an?asynchronous?function
????connection->send(send_stream?[](const?SimpleWeb::error_code?&ec)?{
??????if(ec)?{
????????cout?<“Server:?Error?sending?message.?“?<<
????????????//?See?http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html?Error?Codes?for?error?code?meanings
????????????“Error:?“?<??????}
????});
??};
??echo.on_open?=?[](shared_ptr?connection)?{
????cout?<“Server:?Opened?connection?“?<??};
??//?See?RFC?6455?7.4.1.?for?status?codes
??echo.on_close?=?[](shared_ptr?connection?int?status?const?string?&?/*reason*/)?{
????cout?<“Server:?Closed?connection?“?<??};
??//?See?http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html?Error?Codes?for?error?code?meanings
??echo.on_error?=?[](shared_ptr?connection?const?SimpleWeb::error_code?&ec)?{
????cout?<“Server:?Error?in?connection?“?<?????????<“Error:?“?<??};
??//?Example?2:?Echo?thrice
??//?Demonstrating?queuing?of?messages?by?sending?a?received?message?three?times?back?to?the?client.
??//?Concurrent?send?operations?are?automatically?queued?by?the?library.
??//?Test?with?the?following?javascript:
??//???var?wss=new?WebSocket(“wss://localhost:8080/echo_thrice“);
??//???wss.onmessage=function(evt){console.log(evt.data);};
??//???wss.send(“test“);
??auto?&echo_thrice?=?server.endpoint[“^/echo_thrice/?$“];
??echo_thrice.on_message?=?[](shared_ptr?connection?shared_ptr?message)?{
????auto?send_stream?=?make_shared();
????*send_stream?<string();
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-06-12?09:20??Simple-WebSocket-Server-master\
?????文件?????????222??2018-06-12?09:20??Simple-WebSocket-Server-master\.clang-format
?????文件?????????369??2018-06-12?09:20??Simple-WebSocket-Server-master\.gitignore
?????文件?????????642??2018-06-12?09:20??Simple-WebSocket-Server-master\.travis.yml
?????文件????????3108??2018-06-12?09:20??Simple-WebSocket-Server-master\CMakeLists.txt
?????文件????????1093??2018-06-12?09:20??Simple-WebSocket-Server-master\LICENSE
?????文件????????1978??2018-06-12?09:20??Simple-WebSocket-Server-master\README.md
?????文件???????27874??2018-06-12?09:20??Simple-WebSocket-Server-master\client_ws.hpp
?????文件????????3564??2018-06-12?09:20??Simple-WebSocket-Server-master\client_wss.hpp
?????文件????????8286??2018-06-12?09:20??Simple-WebSocket-Server-master\crypto.hpp
?????文件?????????414??2018-06-12?09:20??Simple-WebSocket-Server-master\ja
?????文件???????29869??2018-06-12?09:20??Simple-WebSocket-Server-master\server_ws.hpp
?????文件????????2877??2018-06-12?09:20??Simple-WebSocket-Server-master\server_wss.hpp
?????文件????????8016??2018-06-12?09:20??Simple-WebSocket-Server-master\status_code.hpp
?????目錄???????????0??2018-06-12?09:20??Simple-WebSocket-Server-master\tests\
?????文件?????????467??2018-06-12?09:20??Simple-WebSocket-Server-master\tests\CMakeLists.txt
?????文件????????3825??2018-06-12?09:20??Simple-WebSocket-Server-master\tests\crypto_test.cpp
?????文件????????8683??2018-06-12?09:20??Simple-WebSocket-Server-master\tests\io_test.cpp
?????文件????????5367??2018-06-12?09:20??Simple-WebSocket-Server-master\tests\parse_test.cpp
?????文件???????12261??2018-06-12?09:20??Simple-WebSocket-Server-master\utility.hpp
?????文件????????5998??2018-06-12?09:20??Simple-WebSocket-Server-master\ws_examples.cpp
?????文件????????6167??2018-06-12?09:20??Simple-WebSocket-Server-master\wss_examples.cpp
評論
共有 條評論