From ee8d28dbeff3470111384635dc22e25eb9b71997 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Tue, 17 Sep 2019 09:48:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E7=8B=AC=E6=8F=90=E4=BE=9Bwebsocket?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=A8=8B=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/main.cpp | 6 +-- src/Http/WebSocketSession.h | 32 -------------- tests/test_httpApi.cpp | 4 +- tests/test_server.cpp | 12 +++--- tests/test_wsServer.cpp | 86 +++++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 43 deletions(-) create mode 100644 tests/test_wsServer.cpp diff --git a/server/main.cpp b/server/main.cpp index cb64df2e..fa72cc9b 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -270,13 +270,13 @@ int main(int argc,char *argv[]) { shellSrv->start(shellPort); rtspSrv->start(rtspPort);//默认554 rtmpSrv->start(rtmpPort);//默认1935 - //http服务器,支持websocket - httpSrv->start(httpPort);//默认80 + //http服务器 + httpSrv->start(httpPort);//默认80 //如果支持ssl,还可以开启https服务器 TcpServer::Ptr httpsSrv(new TcpServer()); //https服务器,支持websocket - httpsSrv->start(httpsPort);//默认443 + httpsSrv->start(httpsPort);//默认443 //支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问 TcpServer::Ptr rtspSSLSrv(new TcpServer()); diff --git a/src/Http/WebSocketSession.h b/src/Http/WebSocketSession.h index 6e55d224..fe4d443a 100644 --- a/src/Http/WebSocketSession.h +++ b/src/Http/WebSocketSession.h @@ -199,37 +199,5 @@ private: std::shared_ptr _session; }; -/** -* 回显会话 -*/ -class EchoSession : public TcpSession { -public: - EchoSession(const Socket::Ptr &pSock) : TcpSession(pSock){ - DebugL; - } - virtual ~EchoSession(){ - DebugL; - } - - void attachServer(const TcpServer &server) override{ - DebugL << getIdentifier() << " " << TcpSession::getIdentifier(); - } - void onRecv(const Buffer::Ptr &buffer) override { - //回显数据 - send(buffer); - } - void onError(const SockException &err) override{ - WarnL << err.what(); - } - //每隔一段时间触发,用来做超时管理 - void onManager() override{ - DebugL; - } -}; - - -typedef WebSocketSession EchoWebSocketSession; -typedef WebSocketSession SSLEchoWebSocketSession; - #endif //ZLMEDIAKIT_WEBSOCKETSESSION_H diff --git a/tests/test_httpApi.cpp b/tests/test_httpApi.cpp index bf8ff4d3..8694eac6 100644 --- a/tests/test_httpApi.cpp +++ b/tests/test_httpApi.cpp @@ -124,11 +124,11 @@ int main(int argc,char *argv[]){ //开启http服务器 TcpServer::Ptr httpSrv(new TcpServer()); - httpSrv->start(mINI::Instance()[Http::kPort]);//默认80 + httpSrv->start(mINI::Instance()[Http::kPort]);//默认80 //如果支持ssl,还可以开启https服务器 TcpServer::Ptr httpsSrv(new TcpServer()); - httpsSrv->start(mINI::Instance()[Http::kSSLPort]);//默认443 + httpsSrv->start(mINI::Instance()[Http::kSSLPort]);//默认443 InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1" << endl; diff --git a/tests/test_server.cpp b/tests/test_server.cpp index 6ba58bd8..6c9d6bf7 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -300,13 +300,13 @@ int main(int argc,char *argv[]) { shellSrv->start(shellPort); rtspSrv->start(rtspPort);//默认554 rtmpSrv->start(rtmpPort);//默认1935 - //http服务器,支持websocket - httpSrv->start(httpPort);//默认80 + //http服务器 + httpSrv->start(httpPort);//默认80 //如果支持ssl,还可以开启https服务器 TcpServer::Ptr httpsSrv(new TcpServer()); - //https服务器,支持websocket - httpsSrv->start(httpsPort);//默认443 + //https服务器 + httpsSrv->start(httpsPort);//默认443 //支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问 TcpServer::Ptr rtspSSLSrv(new TcpServer()); @@ -332,12 +332,12 @@ int main(int argc,char *argv[]) { } if(httpPort != mINI::Instance()[Http::kPort].as()){ httpPort = mINI::Instance()[Http::kPort]; - httpSrv->start(httpPort); + httpSrv->start(httpPort); InfoL << "重启http服务器" << httpPort; } if(httpsPort != mINI::Instance()[Http::kSSLPort].as()){ httpsPort = mINI::Instance()[Http::kSSLPort]; - httpsSrv->start(httpsPort); + httpsSrv->start(httpsPort); InfoL << "重启https服务器" << httpsPort; } diff --git a/tests/test_wsServer.cpp b/tests/test_wsServer.cpp new file mode 100644 index 00000000..5d7a5362 --- /dev/null +++ b/tests/test_wsServer.cpp @@ -0,0 +1,86 @@ +/* + * MIT License + * + * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com> + * + * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include "Util/MD5.h" +#include "Util/logger.h" +#include "Http/WebSocketSession.h" +using namespace std; +using namespace toolkit; +using namespace mediakit; + +/** +* 回显会话 +*/ +class EchoSession : public TcpSession { +public: + EchoSession(const Socket::Ptr &pSock) : TcpSession(pSock){ + DebugL; + } + virtual ~EchoSession(){ + DebugL; + } + + void attachServer(const TcpServer &server) override{ + DebugL << getIdentifier() << " " << TcpSession::getIdentifier(); + } + void onRecv(const Buffer::Ptr &buffer) override { + //回显数据 + send(buffer); + } + void onError(const SockException &err) override{ + WarnL << err.what(); + } + //每隔一段时间触发,用来做超时管理 + void onManager() override{ + DebugL; + } +}; + +int main(int argc, char *argv[]) { + //设置日志 + Logger::Instance().add(std::make_shared()); + Logger::Instance().setWriter(std::make_shared()); + + SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data()); + + TcpServer::Ptr httpSrv(new TcpServer()); + //http服务器,支持websocket + httpSrv->start>(80);//默认80 + + TcpServer::Ptr httpsSrv(new TcpServer()); + //https服务器,支持websocket + httpsSrv->start>(443);//默认443 + + //设置退出信号处理函数 + static semaphore sem; + signal(SIGINT, [](int) { sem.post(); });// 设置退出信号 + sem.wait(); + return 0; +} +