资源简介
一个用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++ STL实现
- c++stl标准库源码
- 标准C++STL源码剖析(侯捷著)(非扫
- stl读取分层
- C++STL帮助文档
- C++17 STL Cookbook
- 数据结构C++语言描述--应用标准模板库
- c++标准库STL手册
- STL文件读取、显示、操作、控制、分
- 单项选择题标准化考试系统的设计C
- libcstl数据结构和常用的算法库
- 用过最好用的C/C++/STLchm格式的帮助文
- XYZ和STL文件MFC显示
- C++ STL参考手册
- c++标准程序库《》侯杰
- 传智播客扫地僧C++STL.zip
- mfc STL格式形体的读取与显示
- Using the C++ Standard Template Libraries《C+
- C++17 stl cook book 中文版
- 用C++stl与QT实现的A*算法Demo
- C++课件stl及
- 标准模板库自修教程与参考手册 STL进
- opengl对stl文件的读取及显示。
- C++STL库查询手册工具书,指导教程
- RP系统中STL模型的分割与拼接研究
- MFC+OpenGL 读取并显示STL三维模型
- C++Primer中文版(第4版)
- 基于MFC对话框扫雷程序
- STL文件的分割与拼接处理软件开发
- C++_标准模板库(STL)
川公网安备 51152502000135号
评论
共有 条评论