2019-09-17 09:48:20 +08:00
|
|
|
|
/*
|
|
|
|
|
* 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 {
|
|
|
|
|
//回显数据
|
2020-01-15 15:11:42 +08:00
|
|
|
|
send("from EchoSession:");
|
2019-09-17 09:48:20 +08:00
|
|
|
|
send(buffer);
|
|
|
|
|
}
|
|
|
|
|
void onError(const SockException &err) override{
|
|
|
|
|
WarnL << err.what();
|
|
|
|
|
}
|
|
|
|
|
//每隔一段时间触发,用来做超时管理
|
|
|
|
|
void onManager() override{
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-15 15:11:42 +08:00
|
|
|
|
|
|
|
|
|
class EchoSessionWithUrl : public TcpSession {
|
|
|
|
|
public:
|
|
|
|
|
EchoSessionWithUrl(const Socket::Ptr &pSock) : TcpSession(pSock){
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
virtual ~EchoSessionWithUrl(){
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void attachServer(const TcpServer &server) override{
|
|
|
|
|
DebugL << getIdentifier() << " " << TcpSession::getIdentifier();
|
|
|
|
|
}
|
|
|
|
|
void onRecv(const Buffer::Ptr &buffer) override {
|
|
|
|
|
//回显数据
|
|
|
|
|
send("from EchoSessionWithUrl:");
|
|
|
|
|
send(buffer);
|
|
|
|
|
}
|
|
|
|
|
void onError(const SockException &err) override{
|
|
|
|
|
WarnL << err.what();
|
|
|
|
|
}
|
|
|
|
|
//每隔一段时间触发,用来做超时管理
|
|
|
|
|
void onManager() override{
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 此对象可以根据websocket 客户端访问的url选择创建不同的对象
|
|
|
|
|
*/
|
|
|
|
|
struct EchoSessionCreator {
|
|
|
|
|
//返回的TcpSession必须派生于SendInterceptor,可以返回null(拒绝连接)
|
|
|
|
|
TcpSession::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock) {
|
|
|
|
|
// return nullptr;
|
|
|
|
|
if (header.Url() == "/") {
|
|
|
|
|
return std::make_shared<TcpSessionTypeImp<EchoSession> >(header, parent, pSock);
|
|
|
|
|
}
|
|
|
|
|
return std::make_shared<TcpSessionTypeImp<EchoSessionWithUrl> >(header, parent, pSock);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-17 09:48:20 +08:00
|
|
|
|
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
|
2020-01-15 15:11:42 +08:00
|
|
|
|
httpSrv->start<WebSocketSessionBase<EchoSessionCreator,HttpSession> >(80);//默认80
|
2019-09-17 09:48:20 +08:00
|
|
|
|
|
|
|
|
|
TcpServer::Ptr httpsSrv(new TcpServer());
|
|
|
|
|
//https服务器,支持websocket
|
2020-01-15 15:11:42 +08:00
|
|
|
|
httpsSrv->start<WebSocketSessionBase<EchoSessionCreator,HttpsSession> >(443);//默认443
|
|
|
|
|
|
|
|
|
|
TcpServer::Ptr httpSrvOld(new TcpServer());
|
|
|
|
|
//兼容之前的代码(但是不支持根据url选择生成TcpSession类型)
|
|
|
|
|
httpSrvOld->start<WebSocketSession<EchoSession,HttpSession> >(8080);
|
|
|
|
|
|
|
|
|
|
DebugL << "请打开网页:http://www.websocket-test.com/,进行测试";
|
|
|
|
|
DebugL << "连接 ws://127.0.0.1/xxxx,ws://127.0.0.1/ 测试的效果将不同,支持根据url选择不同的处理逻辑";
|
2019-09-17 09:48:20 +08:00
|
|
|
|
|
2019-09-17 10:11:09 +08:00
|
|
|
|
|
2019-09-17 09:48:20 +08:00
|
|
|
|
//设置退出信号处理函数
|
|
|
|
|
static semaphore sem;
|
|
|
|
|
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
|
|
|
|
|
sem.wait();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|