单独提供websocket服务器程序

This commit is contained in:
xiongziliang 2019-09-17 09:48:20 +08:00
parent 4d84ae92f9
commit ee8d28dbef
5 changed files with 97 additions and 43 deletions

View File

@ -270,13 +270,13 @@ int main(int argc,char *argv[]) {
shellSrv->start<ShellSession>(shellPort); shellSrv->start<ShellSession>(shellPort);
rtspSrv->start<RtspSession>(rtspPort);//默认554 rtspSrv->start<RtspSession>(rtspPort);//默认554
rtmpSrv->start<RtmpSession>(rtmpPort);//默认1935 rtmpSrv->start<RtmpSession>(rtmpPort);//默认1935
//http服务器,支持websocket //http服务器
httpSrv->start<EchoWebSocketSession>(httpPort);//默认80 httpSrv->start<HttpSession>(httpPort);//默认80
//如果支持ssl还可以开启https服务器 //如果支持ssl还可以开启https服务器
TcpServer::Ptr httpsSrv(new TcpServer()); TcpServer::Ptr httpsSrv(new TcpServer());
//https服务器,支持websocket //https服务器,支持websocket
httpsSrv->start<SSLEchoWebSocketSession>(httpsPort);//默认443 httpsSrv->start<HttpsSession>(httpsPort);//默认443
//支持ssl加密的rtsp服务器可用于诸如亚马逊echo show这样的设备访问 //支持ssl加密的rtsp服务器可用于诸如亚马逊echo show这样的设备访问
TcpServer::Ptr rtspSSLSrv(new TcpServer()); TcpServer::Ptr rtspSSLSrv(new TcpServer());

View File

@ -199,37 +199,5 @@ private:
std::shared_ptr<SessionImp> _session; std::shared_ptr<SessionImp> _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<EchoSession,HttpSession> EchoWebSocketSession;
typedef WebSocketSession<EchoSession,HttpsSession> SSLEchoWebSocketSession;
#endif //ZLMEDIAKIT_WEBSOCKETSESSION_H #endif //ZLMEDIAKIT_WEBSOCKETSESSION_H

View File

@ -124,11 +124,11 @@ int main(int argc,char *argv[]){
//开启http服务器 //开启http服务器
TcpServer::Ptr httpSrv(new TcpServer()); TcpServer::Ptr httpSrv(new TcpServer());
httpSrv->start<EchoWebSocketSession>(mINI::Instance()[Http::kPort]);//默认80 httpSrv->start<HttpSession>(mINI::Instance()[Http::kPort]);//默认80
//如果支持ssl还可以开启https服务器 //如果支持ssl还可以开启https服务器
TcpServer::Ptr httpsSrv(new TcpServer()); TcpServer::Ptr httpsSrv(new TcpServer());
httpsSrv->start<SSLEchoWebSocketSession>(mINI::Instance()[Http::kSSLPort]);//默认443 httpsSrv->start<HttpsSession>(mINI::Instance()[Http::kSSLPort]);//默认443
InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1" << endl; InfoL << "你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1" << endl;

View File

@ -300,13 +300,13 @@ int main(int argc,char *argv[]) {
shellSrv->start<ShellSession>(shellPort); shellSrv->start<ShellSession>(shellPort);
rtspSrv->start<RtspSession>(rtspPort);//默认554 rtspSrv->start<RtspSession>(rtspPort);//默认554
rtmpSrv->start<RtmpSession>(rtmpPort);//默认1935 rtmpSrv->start<RtmpSession>(rtmpPort);//默认1935
//http服务器,支持websocket //http服务器
httpSrv->start<EchoWebSocketSession>(httpPort);//默认80 httpSrv->start<HttpSession>(httpPort);//默认80
//如果支持ssl还可以开启https服务器 //如果支持ssl还可以开启https服务器
TcpServer::Ptr httpsSrv(new TcpServer()); TcpServer::Ptr httpsSrv(new TcpServer());
//https服务器,支持websocket //https服务器
httpsSrv->start<SSLEchoWebSocketSession>(httpsPort);//默认443 httpsSrv->start<HttpsSession>(httpsPort);//默认443
//支持ssl加密的rtsp服务器可用于诸如亚马逊echo show这样的设备访问 //支持ssl加密的rtsp服务器可用于诸如亚马逊echo show这样的设备访问
TcpServer::Ptr rtspSSLSrv(new TcpServer()); TcpServer::Ptr rtspSSLSrv(new TcpServer());
@ -332,12 +332,12 @@ int main(int argc,char *argv[]) {
} }
if(httpPort != mINI::Instance()[Http::kPort].as<uint16_t>()){ if(httpPort != mINI::Instance()[Http::kPort].as<uint16_t>()){
httpPort = mINI::Instance()[Http::kPort]; httpPort = mINI::Instance()[Http::kPort];
httpSrv->start<EchoWebSocketSession>(httpPort); httpSrv->start<HttpSession>(httpPort);
InfoL << "重启http服务器" << httpPort; InfoL << "重启http服务器" << httpPort;
} }
if(httpsPort != mINI::Instance()[Http::kSSLPort].as<uint16_t>()){ if(httpsPort != mINI::Instance()[Http::kSSLPort].as<uint16_t>()){
httpsPort = mINI::Instance()[Http::kSSLPort]; httpsPort = mINI::Instance()[Http::kSSLPort];
httpsSrv->start<SSLEchoWebSocketSession>(httpsPort); httpsSrv->start<HttpsSession>(httpsPort);
InfoL << "重启https服务器" << httpsPort; InfoL << "重启https服务器" << httpsPort;
} }

86
tests/test_wsServer.cpp Normal file
View File

@ -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 <signal.h>
#include <string>
#include <iostream>
#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<ConsoleChannel>());
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data());
TcpServer::Ptr httpSrv(new TcpServer());
//http服务器,支持websocket
httpSrv->start<WebSocketSession<EchoSession,HttpSession>>(80);//默认80
TcpServer::Ptr httpsSrv(new TcpServer());
//https服务器,支持websocket
httpsSrv->start<WebSocketSession<EchoSession,HttpsSession>>(443);//默认443
//设置退出信号处理函数
static semaphore sem;
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
sem.wait();
return 0;
}