no message
This commit is contained in:
parent
7f9c6c2733
commit
b52139cb25
@ -13,3 +13,8 @@
|
||||
#define WM_USER_TCP_CLIENT_CLOSE (WM_USER + 11)
|
||||
#define WM_USER_TCP_SERVER_CLOSE (WM_USER + 12)
|
||||
#define WM_USER_UDP_GROUP_CLOSE (WM_USER + 13)
|
||||
|
||||
#define WM_ADD_WEBSOCKET_SERVER (WM_USER + 14)
|
||||
#define WM_ADD_WEBSOCKET_SERVER_CLOSE (WM_USER + 15)
|
||||
#define WM_ADD_WEBSOCKET_CLIENT (WM_USER + 16)
|
||||
#define WM_ADD_WEBSOCKET_CLIENT_CLOSE (WM_USER + 17)
|
||||
|
@ -10,6 +10,7 @@
|
||||
#include "base_form.h"
|
||||
#include "tcp_client.h"
|
||||
#include "udp_libevent.h"
|
||||
#include "websocket_client.h"
|
||||
#include "utils.h"
|
||||
#include <string>
|
||||
|
||||
@ -106,7 +107,6 @@ void NewMonitorForm::InitWindow()
|
||||
ui::Button* btn_add_new = dynamic_cast<ui::Button*>(FindControl(L"open_button"));
|
||||
if (nullptr != btn_add_new) {
|
||||
btn_add_new->AttachClick([this](ui::EventArgs*) {
|
||||
|
||||
if(m_combo_type->GetText() == L"uart"){
|
||||
UINT PortNum = 0;
|
||||
for (int i = 3; m_uart_select_combo->GetText()[i] != '\0'; i++) //ת»»ÎªÊý×Ö
|
||||
@ -189,7 +189,6 @@ void NewMonitorForm::InitWindow()
|
||||
printf("postmessage error :%d\r\n", GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
if (m_combo_type->GetText() == L"udp group") {
|
||||
wprintf(L"%s\r\n", m_ip_select->GetText().c_str());
|
||||
wprintf(L"%s\r\n", m_port_select->GetText().c_str());
|
||||
@ -208,7 +207,24 @@ void NewMonitorForm::InitWindow()
|
||||
printf("postmessage error :%d\r\n", GetLastError());
|
||||
}
|
||||
}
|
||||
if (m_combo_type->GetText() == L"websocket client") {
|
||||
wprintf(L"%s\r\n", m_ip_select->GetText().c_str());
|
||||
wprintf(L"%s\r\n", m_port_select->GetText().c_str());
|
||||
int port = atoi(wstring2string(m_port_select->GetText()).c_str());
|
||||
UdpDataGramLibevent* udp = new UdpDataGramLibevent("0.0.0.0",
|
||||
port, wstring2string(m_ip_select->GetText().c_str()));
|
||||
TcpServerInfo* p = new TcpServerInfo;
|
||||
p->ip = m_ip_select->GetText();
|
||||
p->port = port;
|
||||
p->socket_fd = udp->SocketFD();
|
||||
printf("¿ªÆô·þÎñ¶Ë %d \r\n", p->socket_fd);
|
||||
|
||||
auto succ = ::PostMessage(m_parent->GetHWND(),
|
||||
WM_ADD_WEBSOCKET_SERVER, (WPARAM)p, (LPARAM)udp);
|
||||
if (!succ) {
|
||||
printf("postmessage error :%d\r\n", GetLastError());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
@ -81,9 +81,9 @@
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<LinkIncremental>true</LinkIncremental>
|
||||
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);..\..\;$(ProjectDir)third\include\libevent\include;$(ProjectDir);$(ProjectDir)third\include\lua;$(ProjectDir)third\include\;D:\include</IncludePath>
|
||||
<IncludePath>$(VC_IncludePath);$(WindowsSDK_IncludePath);..\..\;$(ProjectDir)third\include\libevent\include;$(ProjectDir);$(ProjectDir)third\include\lua;$(ProjectDir)third\include\;D:\include;I:\home\.conan\data\OPENSSL\1.1.1\_\_\build\149ed843ea788702da310f9d8f34c34c2a72994a\pkgsrc\inc32</IncludePath>
|
||||
<ReferencePath>$(ReferencePath)</ReferencePath>
|
||||
<LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(ProjectDir)third\lib</LibraryPath>
|
||||
<LibraryPath>$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64);$(ProjectDir)third\lib;I:\home\.conan\data\OPENSSL\1.1.1\_\_\build\149ed843ea788702da310f9d8f34c34c2a72994a\pkgsrc\out32dll</LibraryPath>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<LinkIncremental>false</LinkIncremental>
|
||||
@ -127,11 +127,12 @@
|
||||
<PreprocessorDefinitions>_WEBSOCKETPP_CPP11_TYPE_TRAITS_;_WEBSOCKETPP_CPP11_RANDOM_DEVICE_;ASIO_STANDALONE ;_WINSOCK_DEPRECATED_NO_WARNINGS;_CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;_WSPIAPI_H_;_WINSOCKAPI_;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
|
||||
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>ws2_32.lib;event_extra.lib;event_core.lib;event.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;lua53.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>ws2_32.lib;event_extra.lib;event_core.lib;event.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;lua53.lib;Bcrypt.lib;ssleay32.lib;libeay32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
@ -181,8 +182,10 @@
|
||||
<ClCompile Include="lua_bind.cpp" />
|
||||
<ClCompile Include="websocket_client.cpp" />
|
||||
<ClCompile Include="websocket_client_form.cpp" />
|
||||
<ClCompile Include="websocket_server.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\..\..\cppnetwork\websocket_client.h" />
|
||||
<ClInclude Include="base_form.h" />
|
||||
<ClInclude Include="global.h" />
|
||||
<ClInclude Include="lua_bind.h" />
|
||||
@ -200,7 +203,6 @@
|
||||
<ClInclude Include="udp_group_form.h" />
|
||||
<ClInclude Include="udp_libevent.h" />
|
||||
<ClInclude Include="utils.h" />
|
||||
<ClInclude Include="websocket_client.h" />
|
||||
<ClInclude Include="websocket_client_form.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
@ -66,10 +66,13 @@
|
||||
<ClCompile Include="udp_group_form.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="websocket_client_form.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="websocket_client.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="websocket_client_form.cpp">
|
||||
<ClCompile Include="websocket_server.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
@ -125,10 +128,10 @@
|
||||
<ClInclude Include="udp_group_form.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="websocket_client.h">
|
||||
<ClInclude Include="websocket_client_form.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="websocket_client_form.h">
|
||||
<ClInclude Include="..\..\..\cppnetwork\websocket_client.h">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
|
@ -1,70 +1,255 @@
|
||||
|
||||
#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;
|
||||
#include "websocket_client.h"
|
||||
|
||||
// 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) {
|
||||
void on_message(WebsocketClient* 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);
|
||||
// c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
|
||||
if (ec) {
|
||||
std::cout << "Echo failed because: " << ec.message() << std::endl;
|
||||
}
|
||||
if (c->m_onread != nullptr) {
|
||||
c->m_onread(c,msg->get_payload());
|
||||
}
|
||||
}
|
||||
|
||||
int main11(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));
|
||||
|
||||
int WebsocketClient::SendMsg(const char * str,uint32_t len,
|
||||
websocketpp::frame::opcode::value opcode)
|
||||
{
|
||||
if(this->m_status != WebsocketClient::CONNECTED)
|
||||
return -1;
|
||||
if(m_tls){
|
||||
websocketpp::lib::error_code ec;
|
||||
client::connection_ptr con = c.get_connection(uri, ec);
|
||||
this->m_client_tls.send(m_conn_tls,str,len,opcode,ec);
|
||||
if (ec) {
|
||||
std::cout << "Echo failed because: " << ec.message() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
}else{
|
||||
websocketpp::lib::error_code ec;
|
||||
this->m_client.send(m_conn,str,len,opcode,ec);
|
||||
if (ec) {
|
||||
std::cout << "Echo failed because: " << ec.message() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void on_open(WebsocketClient * c, websocketpp::connection_hdl hdl) {
|
||||
if(c->m_tls){
|
||||
TlsClient::connection_ptr con = c->m_client_tls.get_con_from_hdl(hdl);
|
||||
uint32_t usocket = con->get_raw_socket().native_handle();
|
||||
auto m_server = con->get_response_header("Server");
|
||||
std::cout<<"open from server"<<m_server<<std::endl;
|
||||
c->m_status = WebsocketClient::CONNECTED;
|
||||
if (c->m_on_connected != nullptr) {
|
||||
c->m_on_connected(c);
|
||||
}
|
||||
}else{
|
||||
Client::connection_ptr con = c->m_client.get_con_from_hdl(hdl);
|
||||
uint32_t usocket = con->get_raw_socket().native_handle();
|
||||
auto m_server = con->get_response_header("Server");
|
||||
std::cout<<"open from server"<<m_server<<std::endl;
|
||||
c->m_status = WebsocketClient::CONNECTED;
|
||||
if (c->m_on_connected != nullptr) {
|
||||
c->m_on_connected(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void on_close(WebsocketClient * c, websocketpp::connection_hdl hdl) {
|
||||
// m_status = "Open";
|
||||
|
||||
// client::connection_ptr con = c->get_con_from_hdl(hdl);
|
||||
// m_server = con->get_response_header("Server");
|
||||
c->m_status = WebsocketClient::CLOSED;
|
||||
std::cout<<"on_close"<<std::endl;
|
||||
if (c->m_on_disconnected != nullptr) {
|
||||
c->m_on_disconnected(c,WebsocketClient::CloseReason::LOCAL_CLOSED);
|
||||
}
|
||||
}
|
||||
|
||||
void on_fail(WebsocketClient * c, websocketpp::connection_hdl hdl) {
|
||||
std::cout<<"on_fail"<<std::endl;
|
||||
Client::connection_ptr con = c->m_client.get_con_from_hdl(hdl);
|
||||
auto state = con->get_state();
|
||||
if(state == websocketpp::session::state::closed)
|
||||
std::cout<<state <<" on_fail "<<std::endl;
|
||||
c->m_status = WebsocketClient::FAIL;
|
||||
if (c->m_on_disconnected != nullptr) {
|
||||
c->m_on_disconnected(c,WebsocketClient::CloseReason::PEER_CLOSED);
|
||||
}
|
||||
}
|
||||
|
||||
WebsocketClient::~WebsocketClient(){
|
||||
|
||||
this->m_status = WebsocketClient::STOP;
|
||||
m_client.stop();
|
||||
m_thread->join();
|
||||
}
|
||||
|
||||
WebsocketClient::WebsocketClient(std::string url,bool tls)
|
||||
{
|
||||
m_tls = tls;
|
||||
m_auto_reconn = false;
|
||||
m_url = url;
|
||||
if(m_tls){
|
||||
// Set logging to be pretty verbose (everything except message payloads)
|
||||
m_client_tls.set_access_channels(websocketpp::log::alevel::all);
|
||||
m_client_tls.clear_access_channels(websocketpp::log::alevel::frame_payload);
|
||||
m_client_tls.set_tls_init_handler([this](websocketpp::connection_hdl){
|
||||
return websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::tlsv1);
|
||||
});
|
||||
// Initialize ASIO
|
||||
m_client_tls.init_asio();
|
||||
m_thread = new std::thread([this]()
|
||||
{
|
||||
while (this->m_status != STOP)
|
||||
{
|
||||
m_client_tls.set_message_handler(bind(&on_message, this, ::_1, ::_2));
|
||||
websocketpp::lib::error_code ec;
|
||||
std::cout<<"1"<<std::endl;
|
||||
m_conn_tls = m_client_tls.get_connection(this->m_url, ec);
|
||||
m_conn_tls->set_open_handler(websocketpp::lib::bind(
|
||||
&on_open,
|
||||
this,
|
||||
websocketpp::lib::placeholders::_1
|
||||
));
|
||||
m_conn_tls->set_fail_handler(websocketpp::lib::bind(
|
||||
&on_fail,
|
||||
this,
|
||||
websocketpp::lib::placeholders::_1
|
||||
));
|
||||
m_conn_tls->set_close_handler(websocketpp::lib::bind(
|
||||
&on_close,
|
||||
this,
|
||||
websocketpp::lib::placeholders::_1
|
||||
));
|
||||
if (ec) {
|
||||
std::cout << "could not create connection because: " << ec.message() << std::endl;
|
||||
return 0;
|
||||
this->m_status = Status::FAIL;
|
||||
}
|
||||
std::cout<<"2"<<std::endl;
|
||||
|
||||
// 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);
|
||||
|
||||
TlsClient::connection_ptr ptr = m_client_tls.connect(m_conn_tls);
|
||||
if(ptr->get_state() != websocketpp::session::state::open)
|
||||
std::cout<<ptr->get_state() <<" websocketpp::session::state "<<std::endl;
|
||||
// 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();
|
||||
std::cout<<"3 " << ptr << " " <<m_conn_tls <<std::endl;
|
||||
this->m_status = WebsocketClient::CONNECTING;
|
||||
while((this->m_status != WebsocketClient::FAIL) &&
|
||||
(this->m_status != WebsocketClient::CLOSED)
|
||||
&&(this->m_status != WebsocketClient::STOP)
|
||||
){
|
||||
try{
|
||||
// while(this->m_status == WebsocketClient::CONNECTED){
|
||||
int count_of_handler = this->m_client_tls.run();
|
||||
// std::cout<<"count_of_handler: " << count_of_handler<<std::endl;
|
||||
// }
|
||||
// run应该只执行一次就会退出
|
||||
}catch(std::exception e){
|
||||
std::cout<< "run exception"<< e.what();
|
||||
}
|
||||
catch (websocketpp::exception const& e) {
|
||||
std::cout << e.what() << std::endl;
|
||||
}
|
||||
Sleep(1000);
|
||||
}
|
||||
std::cout<<"close";
|
||||
this->m_on_disconnected(this,WebsocketClient::CloseReason::LOCAL_CLOSED);
|
||||
});
|
||||
}
|
||||
else{
|
||||
// Set logging to be pretty verbose (everything except message payloads)
|
||||
m_client.set_access_channels(websocketpp::log::alevel::all);
|
||||
m_client.clear_access_channels(websocketpp::log::alevel::frame_payload);
|
||||
|
||||
// Initialize ASIO
|
||||
this->m_client.init_asio();
|
||||
m_thread = new std::thread([this]()
|
||||
{
|
||||
while (this->m_status != STOP)
|
||||
{
|
||||
this->m_client.set_message_handler(bind(&on_message, this, ::_1, ::_2));
|
||||
websocketpp::lib::error_code ec;
|
||||
std::cout<<"1"<<std::endl;
|
||||
m_conn = m_client.get_connection(this->m_url, ec);
|
||||
m_conn->set_open_handler(websocketpp::lib::bind(
|
||||
&on_open,
|
||||
this,
|
||||
websocketpp::lib::placeholders::_1
|
||||
));
|
||||
m_conn->set_fail_handler(websocketpp::lib::bind(
|
||||
&on_fail,
|
||||
this,
|
||||
websocketpp::lib::placeholders::_1
|
||||
));
|
||||
m_conn->set_close_handler(websocketpp::lib::bind(
|
||||
&on_close,
|
||||
this,
|
||||
websocketpp::lib::placeholders::_1
|
||||
));
|
||||
if (ec) {
|
||||
std::cout << "could not create connection because: " << ec.message() << std::endl;
|
||||
this->m_status = Status::FAIL;
|
||||
}
|
||||
std::cout<<"2"<<std::endl;
|
||||
|
||||
// Note that connect here only requests a connection. No network messages are
|
||||
// exchanged until the event loop starts running in the next line.
|
||||
Client::connection_ptr ptr = this->m_client.connect(m_conn);
|
||||
if(ptr->get_state() != websocketpp::session::state::open)
|
||||
std::cout<<ptr->get_state() <<" websocketpp::session::state "<<std::endl;
|
||||
// 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.
|
||||
std::cout<<"3 " << ptr << " " <<m_conn_tls <<std::endl;
|
||||
this->m_status = WebsocketClient::CONNECTING;
|
||||
while((this->m_status != WebsocketClient::FAIL) &&
|
||||
(this->m_status != WebsocketClient::CLOSED)
|
||||
&&(this->m_status != WebsocketClient::STOP)
|
||||
){
|
||||
try{
|
||||
// while(this->m_status == WebsocketClient::CONNECTED){
|
||||
int count_of_handler = this->m_client.run();
|
||||
// std::cout<<"count_of_handler: " << count_of_handler<<std::endl;
|
||||
// }
|
||||
// run应该只执行一次就会退出
|
||||
}catch(std::exception e){
|
||||
std::cout<< "run exception"<< e.what();
|
||||
}
|
||||
}
|
||||
Sleep(1000);
|
||||
}
|
||||
std::cout<<"close";
|
||||
this->m_on_disconnected(this,WebsocketClient::CloseReason::LOCAL_CLOSED);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int WebsocketClient::SetOnConnectedHandler(OnConnectedHandler on_connected){
|
||||
this->m_on_connected = on_connected;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WebsocketClient::SetOnDisConnectedHandler(OnDisConnectedHandler on_disconnected){
|
||||
this->m_on_disconnected = on_disconnected;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WebsocketClient::SetOnReadHandler(OnReadHandler onread){
|
||||
this->m_onread = onread;
|
||||
return 0;
|
||||
}
|
@ -1,7 +1,81 @@
|
||||
/*
|
||||
* @Author: your name
|
||||
* @Date: 2022-02-25 22:06:57
|
||||
* @LastEditTime: 2022-03-06 22:42:35
|
||||
* @LastEditors: Please set LastEditors
|
||||
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
* @FilePath: \test\websocket_client.h
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <websocketpp/config/asio_client.hpp>
|
||||
#include <websocketpp/config/asio_no_tls_client.hpp>
|
||||
#include <websocketpp/client.hpp>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <string.h>
|
||||
|
||||
class WebsocketClient
|
||||
{
|
||||
typedef websocketpp::client<websocketpp::config::asio_client> Client;
|
||||
typedef websocketpp::client<websocketpp::config::asio_tls_client> TlsClient;
|
||||
|
||||
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;
|
||||
|
||||
class WebsocketClient {
|
||||
public:
|
||||
enum CloseReason{
|
||||
PEER_CLOSED = 1,
|
||||
LOCAL_CLOSED = 2
|
||||
};
|
||||
|
||||
typedef std::function<void (WebsocketClient *)> OnConnectedHandler;
|
||||
typedef std::function<void (WebsocketClient *,CloseReason)> OnDisConnectedHandler;
|
||||
typedef std::function<void(WebsocketClient *, std::string )> OnReadHandler;
|
||||
|
||||
enum Status
|
||||
{
|
||||
STOP = 0,
|
||||
CONNECTING = 1,
|
||||
CONNECTED = 2,
|
||||
FAIL = 3,
|
||||
CLOSED = 4,
|
||||
};
|
||||
|
||||
Status State(){
|
||||
return m_status;
|
||||
}
|
||||
WebsocketClient(std::string url,bool tls);
|
||||
~WebsocketClient();
|
||||
int SendMsg(const char * str,uint32_t len,websocketpp::frame::opcode::value);
|
||||
friend void on_fail(WebsocketClient * c, websocketpp::connection_hdl hdl);
|
||||
friend void on_close(WebsocketClient * c, websocketpp::connection_hdl hdl);
|
||||
friend void on_open(WebsocketClient * c, websocketpp::connection_hdl hdl);
|
||||
friend void on_message(WebsocketClient* c, websocketpp::connection_hdl hdl, message_ptr msg);
|
||||
int SetOnConnectedHandler(OnConnectedHandler on_connected);
|
||||
int SetOnDisConnectedHandler(OnDisConnectedHandler on_disconnected);
|
||||
int SetOnReadHandler(OnReadHandler onread);
|
||||
|
||||
private:
|
||||
uint32_t m_socketfd;
|
||||
Status m_status; // 当前服务器状态
|
||||
std::string m_url; // url
|
||||
Client m_client; // 客户端
|
||||
TlsClient m_client_tls;
|
||||
std::thread *m_thread; // 当前活动线程
|
||||
Client::connection_ptr m_conn;
|
||||
TlsClient::connection_ptr m_conn_tls; // 客户端
|
||||
|
||||
bool m_auto_reconn;
|
||||
bool m_tls;
|
||||
OnReadHandler m_onread;
|
||||
OnConnectedHandler m_on_connected;
|
||||
OnDisConnectedHandler m_on_disconnected;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
@ -21,6 +21,8 @@ void WebsocketClientForm::Init()
|
||||
m_check_box_4 = dynamic_cast<ui::CheckBox*>(FindSubControl(L"check_hex_recv"));
|
||||
m_btn_save_lua = dynamic_cast<ui::Button*>(FindSubControl(L"btn_save_lua"));
|
||||
m_btn_close_form = dynamic_cast<ui::Button*>(FindSubControl(L"btn_close_uart"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
LuaDelegate* WebsocketClientForm::LuaVM()
|
||||
|
@ -23,11 +23,7 @@ public:
|
||||
WebsocketClientForm(ui::Window* hwnd, string url, uint32_t port, TcpClientLibevent* p);
|
||||
~WebsocketClientForm();
|
||||
virtual void Init() override;
|
||||
|
||||
|
||||
|
||||
LuaDelegate* LuaVM();
|
||||
|
||||
private:
|
||||
ui::Label* m_label_1;
|
||||
ui::RichEdit* m_rich_edit_1;
|
||||
|
97
examples/proto_debuger/websocket_server.cpp
Normal file
97
examples/proto_debuger/websocket_server.cpp
Normal file
@ -0,0 +1,97 @@
|
||||
#include "websocket_server.h"
|
||||
|
||||
// Define a callback to handle new connection income
|
||||
void on_new_connection(WebsocketServer* s, websocketpp::connection_hdl hdl){
|
||||
std::cout << "on new connection\r\n";
|
||||
WsServer::connection_ptr con = s->m_server->get_con_from_hdl(hdl);
|
||||
auto socket = con->get_raw_socket().native_handle();
|
||||
s->m_clients[socket] = hdl;
|
||||
auto addr = con->get_socket().remote_endpoint().address();
|
||||
auto host = con->get_host();
|
||||
auto uri = con->get_uri();
|
||||
auto resource = con->get_resource();
|
||||
std::cout<<socket << " "<<addr<<" "<< host<<" "<<uri<<" "<<resource<<"\r\n";
|
||||
|
||||
}
|
||||
|
||||
// Define a callback to handle incoming messages
|
||||
void on_connection_close(WebsocketServer* s, websocketpp::connection_hdl hdl){
|
||||
std::cout<<"connection closed\\r\n";
|
||||
WsServer::connection_ptr con = s->m_server->get_con_from_hdl(hdl);
|
||||
auto socket = con->get_raw_socket().native_handle();
|
||||
s->m_clients.erase(socket);
|
||||
}
|
||||
|
||||
// Define a callback to handle incoming messages
|
||||
void on_message(WebsocketServer* s, websocketpp::connection_hdl hdl, message_ptr msg) {
|
||||
std::cout << "on_message called with hdl: " << hdl.lock().get()
|
||||
<< " and message: " << msg->get_payload()
|
||||
<< std::endl;
|
||||
|
||||
// check for a special command to instruct the server to stop listening so
|
||||
// it can be cleanly exited.
|
||||
if (msg->get_payload() == "stop-listening") {
|
||||
s->m_server->stop_listening();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
s->m_server->send(hdl, msg->get_payload(), msg->get_opcode());
|
||||
|
||||
} catch (websocketpp::exception const & e) {
|
||||
std::cout << "Echo failed because: "
|
||||
<< "(" << e.what() << ")" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
WebsocketServer::WebsocketServer(std::string server,uint32_t port){
|
||||
m_port = port;
|
||||
m_server = new WsServer;
|
||||
m_thread = new std::thread([this](){
|
||||
// Set logging settings
|
||||
m_server->set_access_channels(websocketpp::log::alevel::all);
|
||||
m_server->clear_access_channels(websocketpp::log::alevel::frame_payload);
|
||||
// Initialize Asio
|
||||
m_server->init_asio();
|
||||
// Register our message handler
|
||||
m_server->set_open_handler(std::bind(&on_new_connection,this,::_1));
|
||||
m_server->set_message_handler(std::bind(&on_message,this,::_1,::_2));
|
||||
m_server->set_close_handler(std::bind(&on_connection_close,this,::_1));
|
||||
m_server->listen(m_port);
|
||||
// Start the server accept loop
|
||||
m_server->start_accept();
|
||||
// Start the ASIO io_service run loop
|
||||
m_server->run();
|
||||
});
|
||||
}
|
||||
|
||||
int WebsocketServer::SendData(uint32_t fd,const char* dat,int len,
|
||||
websocketpp::frame::opcode::value op){
|
||||
|
||||
auto itr = this->m_clients.find(fd);
|
||||
if(itr != m_clients.end()){
|
||||
std::error_code err;
|
||||
WsServer::connection_ptr con = m_server->get_con_from_hdl(itr->second);
|
||||
m_server->send(itr->second,dat,len,op,err);
|
||||
if(err.message() != ""){
|
||||
std::cout<<"websocket error" << err.message()<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WebsocketServer::SendDataAllClient(const char* dat,int len,
|
||||
websocketpp::frame::opcode::value opcode){
|
||||
|
||||
for(auto itr = m_clients.begin();itr != m_clients.end();itr++){
|
||||
std::error_code err;
|
||||
WsServer::connection_ptr con = m_server->get_con_from_hdl(itr->second);
|
||||
m_server->send(itr->second,dat,len, opcode,err);
|
||||
if(err.message() != ""){
|
||||
std::cout<<"websocket error" << err.message()<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
46
examples/proto_debuger/websocket_server.h
Normal file
46
examples/proto_debuger/websocket_server.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* @Author: your name
|
||||
* @Date: 2022-02-28 21:03:56
|
||||
* @LastEditTime: 2022-02-28 23:24:06
|
||||
* @LastEditors: Please set LastEditors
|
||||
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
* @FilePath: \test\websocket_server.h
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
|
||||
typedef websocketpp::server<websocketpp::config::asio> WsServer;
|
||||
|
||||
|
||||
using websocketpp::lib::placeholders::_1;
|
||||
using websocketpp::lib::placeholders::_2;
|
||||
using websocketpp::lib::bind;
|
||||
typedef WsServer::message_ptr message_ptr;
|
||||
|
||||
|
||||
class WebsocketServer {
|
||||
public:
|
||||
WebsocketServer(std::string server,uint32_t port);
|
||||
friend void on_new_connection(WebsocketServer* s, websocketpp::connection_hdl hdl);
|
||||
friend void on_message(WebsocketServer* s, websocketpp::connection_hdl hdl, message_ptr msg);
|
||||
friend void on_connection_close(WebsocketServer* s, websocketpp::connection_hdl hdl);
|
||||
int SendData(uint32_t,const char *data,int len,websocketpp::frame::opcode::value);
|
||||
int SendDataAllClient(const char *data,int len,websocketpp::frame::opcode::value);
|
||||
|
||||
private:
|
||||
std::string m_url; // 地址
|
||||
uint32_t m_port; // url
|
||||
WsServer *m_server;
|
||||
std::thread *m_thread;
|
||||
std::mutex m_mutex;
|
||||
std::map<uint32_t,websocketpp::connection_hdl> m_clients;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user