websocket客户端销毁时主动发送close信令: #311

This commit is contained in:
xiongziliang 2020-05-25 14:36:58 +08:00
parent 07089ea4e2
commit 5ae887a279
3 changed files with 39 additions and 20 deletions

View File

@ -94,6 +94,20 @@ public:
_onRecv = nullptr; _onRecv = nullptr;
sendRequest(http_url,fTimeOutSec); sendRequest(http_url,fTimeOutSec);
} }
void closeWsClient(){
if(!_onRecv){
//未连接
return;
}
WebSocketHeader header;
header._fin = true;
header._reserved = 0;
header._opcode = CLOSE;
//客户端需要加密
header._mask_flag = true;
WebSocketSplitter::encode(header, nullptr);
}
protected: protected:
//HttpClientImp override //HttpClientImp override
@ -328,7 +342,9 @@ public:
WebSocketClient(ArgsType &&...args) : ClientTypeImp<ClientType,DataType>(std::forward<ArgsType>(args)...){ WebSocketClient(ArgsType &&...args) : ClientTypeImp<ClientType,DataType>(std::forward<ArgsType>(args)...){
_wsClient.reset(new HttpWsClient<ClientType,DataType>(*this)); _wsClient.reset(new HttpWsClient<ClientType,DataType>(*this));
} }
~WebSocketClient() override {} ~WebSocketClient() override {
_wsClient->closeWsClient();
}
/** /**
* startConnect方法 * startConnect方法

View File

@ -59,10 +59,11 @@ int main(int argc, char *argv[]) {
Logger::Instance().add(std::make_shared<ConsoleChannel>()); Logger::Instance().add(std::make_shared<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>()); Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
{
WebSocketClient<EchoTcpClient>::Ptr client = std::make_shared<WebSocketClient<EchoTcpClient> >(); WebSocketClient<EchoTcpClient>::Ptr client = std::make_shared<WebSocketClient<EchoTcpClient> >();
client->startConnect("121.40.165.18",8800); client->startConnect("127.0.0.1", 80);
sem.wait(); sem.wait();
}
return 0; return 0;
} }

View File

@ -96,26 +96,28 @@ int main(int argc, char *argv[]) {
SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data()); SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data());
{
TcpServer::Ptr httpSrv(new TcpServer()); TcpServer::Ptr httpSrv(new TcpServer());
//http服务器,支持websocket //http服务器,支持websocket
httpSrv->start<WebSocketSessionBase<EchoSessionCreator,HttpSession> >(80);//默认80 httpSrv->start<WebSocketSessionBase<EchoSessionCreator, HttpSession> >(80);//默认80
TcpServer::Ptr httpsSrv(new TcpServer()); TcpServer::Ptr httpsSrv(new TcpServer());
//https服务器,支持websocket //https服务器,支持websocket
httpsSrv->start<WebSocketSessionBase<EchoSessionCreator,HttpsSession> >(443);//默认443 httpsSrv->start<WebSocketSessionBase<EchoSessionCreator, HttpsSession> >(443);//默认443
TcpServer::Ptr httpSrvOld(new TcpServer()); TcpServer::Ptr httpSrvOld(new TcpServer());
//兼容之前的代码(但是不支持根据url选择生成TcpSession类型) //兼容之前的代码(但是不支持根据url选择生成TcpSession类型)
httpSrvOld->start<WebSocketSession<EchoSession,HttpSession> >(8080); httpSrvOld->start<WebSocketSession<EchoSession, HttpSession> >(8080);
DebugL << "请打开网页:http://www.websocket-test.com/,进行测试"; DebugL << "请打开网页:http://www.websocket-test.com/,进行测试";
DebugL << "连接 ws://127.0.0.1/xxxxws://127.0.0.1/ 测试的效果将不同支持根据url选择不同的处理逻辑"; DebugL << "连接 ws://127.0.0.1/xxxxws://127.0.0.1/ 测试的效果将不同支持根据url选择不同的处理逻辑";
//设置退出信号处理函数 //设置退出信号处理函数
static semaphore sem; static semaphore sem;
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
sem.wait(); sem.wait();
}
return 0; return 0;
} }