资源简介
一个用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: \““ << message_str << “\“ from “ << connection.get() << endl;
cout << “Server: Sending message \““ << message_str << “\“ to “ << connection.get() << endl;
auto send_stream = make_shared();
*send_stream << message_str;
// 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: “ << ec << “ error message: “ << ec.message() << endl;
}
});
};
echo.on_open = [](shared_ptr connection) {
cout << “Server: Opened connection “ << connection.get() << endl;
};
// See RFC 6455 7.4.1. for status codes
echo.on_close = [](shared_ptr connection int status const string & /*reason*/) {
cout << “Server: Closed connection “ << connection.get() << “ with status code “ << status << endl;
};
// 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 “ << connection.get() << “. “
<< “Error: “ << ec << “ error message: “ << ec.message() << endl;
};
// 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 << message->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
相关资源
- C/C++/STL帮助手册 chm完整离线版
- websocket编程C语言源码
- vc知识库大讲堂c++教程.txt
- 博览网 4视频 侯捷老师的C++内存管理
- 一个好用的C++编写的websocket服务端d
- C++ STL库函数总结纯手打,主要偏向
- VTK源码,读取obj、stl点云,生成重建
- C++课件+STL
- STL模型布尔运算的实现论文 郭开波
- WebsocketPP C++构建Web服务器源代码
- C/C++ STL参考手册 STL帮助文档 中文/英
- c / c++ / cpp / stl 中文帮助文档手册ch
- 数据结构 C++语言描述——应用
- effective stl 中文版高清扫描版.pdf
- c++ websocket服务器和html客户端
-
stm32f103c8t6核心板Jli
nk STli nk 2/2.1 - c++ 手工做一个STL文件
- C++面向对象程序设计自定义类模板库
- stl viewer(查看器)
- C++_STL使用例子大全
- STL参考手册
- SGI—STLSGI C++ 标准程序库源码+文档
- C++_标准模板库(STL) pdf 高清版
- c++ STL编程手册
- C++ STL 标准库源代码
- linux c语言实现websocket
评论
共有 条评论