2019-09-17 09:48:20 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2019-09-17 09:48:20 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2019-09-17 09:48:20 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2019-09-17 09:48:20 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include "Util/MD5.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Http/WebSocketSession.h"
|
2022-02-02 20:34:50 +08:00
|
|
|
|
|
2019-09-17 09:48:20 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
using namespace mediakit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 回显会话
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Echo Session
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:bc2a4e9e]
|
2019-09-17 09:48:20 +08:00
|
|
|
|
*/
|
2022-11-19 09:33:10 +08:00
|
|
|
|
class EchoSession : public Session {
|
2019-09-17 09:48:20 +08:00
|
|
|
|
public:
|
2022-11-19 09:33:10 +08:00
|
|
|
|
EchoSession(const Socket::Ptr &pSock) : Session(pSock){
|
2019-09-17 09:48:20 +08:00
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
virtual ~EchoSession(){
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 11:29:32 +08:00
|
|
|
|
void attachServer(const Server &server) override{
|
2022-11-19 09:33:10 +08:00
|
|
|
|
DebugL << getIdentifier() << " " << Session::getIdentifier();
|
2019-09-17 09:48:20 +08:00
|
|
|
|
}
|
|
|
|
|
void onRecv(const Buffer::Ptr &buffer) override {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 回显数据 [AUTO-TRANSLATED:36769402]
|
|
|
|
|
// Echo Data
|
2020-04-23 17:50:12 +08:00
|
|
|
|
SockSender::send("from EchoSession:");
|
2019-09-17 09:48:20 +08:00
|
|
|
|
send(buffer);
|
|
|
|
|
}
|
|
|
|
|
void onError(const SockException &err) override{
|
|
|
|
|
WarnL << err.what();
|
|
|
|
|
}
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 每隔一段时间触发,用来做超时管理 [AUTO-TRANSLATED:823ffe1f]
|
|
|
|
|
// Triggered at regular intervals, used for timeout management
|
2019-09-17 09:48:20 +08:00
|
|
|
|
void onManager() override{
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-15 15:11:42 +08:00
|
|
|
|
|
2022-11-19 09:33:10 +08:00
|
|
|
|
class EchoSessionWithUrl : public Session {
|
2020-01-15 15:11:42 +08:00
|
|
|
|
public:
|
2022-11-19 09:33:10 +08:00
|
|
|
|
EchoSessionWithUrl(const Socket::Ptr &pSock) : Session(pSock){
|
2020-01-15 15:11:42 +08:00
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
virtual ~EchoSessionWithUrl(){
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 11:29:32 +08:00
|
|
|
|
void attachServer(const Server &server) override{
|
2022-11-19 09:33:10 +08:00
|
|
|
|
DebugL << getIdentifier() << " " << Session::getIdentifier();
|
2020-01-15 15:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
void onRecv(const Buffer::Ptr &buffer) override {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 回显数据 [AUTO-TRANSLATED:36769402]
|
|
|
|
|
// Echo Data
|
2020-04-23 17:50:12 +08:00
|
|
|
|
SockSender::send("from EchoSessionWithUrl:");
|
2020-01-15 15:11:42 +08:00
|
|
|
|
send(buffer);
|
|
|
|
|
}
|
|
|
|
|
void onError(const SockException &err) override{
|
|
|
|
|
WarnL << err.what();
|
|
|
|
|
}
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 每隔一段时间触发,用来做超时管理 [AUTO-TRANSLATED:823ffe1f]
|
|
|
|
|
// Triggered at regular intervals, used for timeout management
|
2020-01-15 15:11:42 +08:00
|
|
|
|
void onManager() override{
|
|
|
|
|
DebugL;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 此对象可以根据websocket 客户端访问的url选择创建不同的对象
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* This object can create different objects based on the URL accessed by the WebSocket client
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:57f51c96]
|
2020-01-15 15:11:42 +08:00
|
|
|
|
*/
|
|
|
|
|
struct EchoSessionCreator {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 返回的Session必须派生于SendInterceptor,可以返回null(拒绝连接) [AUTO-TRANSLATED:014d6a8a]
|
|
|
|
|
// The returned Session must inherit from SendInterceptor, can return null (refuse connection)
|
2023-10-23 20:58:12 +08:00
|
|
|
|
Session::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock, mediakit::WebSocketHeader::Type &type) {
|
2020-01-15 15:11:42 +08:00
|
|
|
|
// return nullptr;
|
2023-06-10 11:04:52 +08:00
|
|
|
|
if (header.url() == "/") {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 可以指定传输方式 [AUTO-TRANSLATED:81ddc417]
|
|
|
|
|
// Transport method can be specified
|
2023-10-23 20:58:12 +08:00
|
|
|
|
// type = mediakit::WebSocketHeader::BINARY;
|
2022-11-19 09:33:10 +08:00
|
|
|
|
return std::make_shared<SessionTypeImp<EchoSession> >(header, parent, pSock);
|
2020-01-15 15:11:42 +08:00
|
|
|
|
}
|
2022-11-19 09:33:10 +08:00
|
|
|
|
return std::make_shared<SessionTypeImp<EchoSessionWithUrl> >(header, parent, pSock);
|
2020-01-15 15:11:42 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-17 09:48:20 +08:00
|
|
|
|
int main(int argc, char *argv[]) {
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置日志 [AUTO-TRANSLATED:50372045]
|
|
|
|
|
// Set log
|
2019-09-17 09:48:20 +08:00
|
|
|
|
Logger::Instance().add(std::make_shared<ConsoleChannel>());
|
|
|
|
|
Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
|
|
|
|
|
|
|
|
|
|
SSL_Initor::Instance().loadCertificate((exeDir() + "ssl.p12").data());
|
|
|
|
|
|
2020-05-25 14:36:58 +08:00
|
|
|
|
{
|
|
|
|
|
TcpServer::Ptr httpSrv(new TcpServer());
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// http服务器,支持websocket [AUTO-TRANSLATED:74a2bdb0]
|
|
|
|
|
// HTTP server, supports WebSocket
|
2020-05-25 14:36:58 +08:00
|
|
|
|
httpSrv->start<WebSocketSessionBase<EchoSessionCreator, HttpSession> >(80);//默认80
|
|
|
|
|
|
|
|
|
|
TcpServer::Ptr httpsSrv(new TcpServer());
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// https服务器,支持websocket [AUTO-TRANSLATED:bc268bb9]
|
|
|
|
|
// HTTPS server, supports WebSocket
|
2020-05-25 14:36:58 +08:00
|
|
|
|
httpsSrv->start<WebSocketSessionBase<EchoSessionCreator, HttpsSession> >(443);//默认443
|
|
|
|
|
|
|
|
|
|
TcpServer::Ptr httpSrvOld(new TcpServer());
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 兼容之前的代码(但是不支持根据url选择生成Session类型) [AUTO-TRANSLATED:d14395bd]
|
|
|
|
|
// Compatible with previous code (but does not support generating Session type based on URL)
|
2020-05-25 14:36:58 +08:00
|
|
|
|
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选择不同的处理逻辑";
|
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 设置退出信号处理函数 [AUTO-TRANSLATED:4f047770]
|
|
|
|
|
// Set exit signal processing function
|
2020-05-25 14:36:58 +08:00
|
|
|
|
static semaphore sem;
|
|
|
|
|
signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
|
|
|
|
|
sem.wait();
|
|
|
|
|
}
|
2019-09-17 10:11:09 +08:00
|
|
|
|
|
2019-09-17 09:48:20 +08:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|