websocket服务器支持在创建器中指定消息类型

通过该修改可以更灵活的指定同端口下不同websocket服务的消息类型
This commit is contained in:
xia-chu 2023-10-23 20:58:12 +08:00
parent ebde21a314
commit a871fc1882
2 changed files with 8 additions and 5 deletions

View File

@ -69,7 +69,7 @@ template <typename SessionType>
class SessionCreator { class SessionCreator {
public: public:
//返回的Session必须派生于SendInterceptor可以返回null //返回的Session必须派生于SendInterceptor可以返回null
toolkit::Session::Ptr operator()(const mediakit::Parser &header, const mediakit::HttpSession &parent, const toolkit::Socket::Ptr &pSock){ toolkit::Session::Ptr operator()(const mediakit::Parser &header, const mediakit::HttpSession &parent, const toolkit::Socket::Ptr &pSock, mediakit::WebSocketHeader::Type &data_type){
return std::make_shared<SessionTypeImp<SessionType> >(header,parent,pSock); return std::make_shared<SessionTypeImp<SessionType> >(header,parent,pSock);
} }
}; };
@ -128,7 +128,8 @@ protected:
*/ */
bool onWebSocketConnect(const mediakit::Parser &header) override{ bool onWebSocketConnect(const mediakit::Parser &header) override{
//创建websocket session类 //创建websocket session类
_session = _creator(header, *this, HttpSessionType::getSock()); auto data_type = DataType;
_session = _creator(header, *this, HttpSessionType::getSock(), data_type);
if (!_session) { if (!_session) {
// 此url不允许创建websocket连接 // 此url不允许创建websocket连接
return false; return false;
@ -140,13 +141,13 @@ protected:
//此处截取数据并进行websocket协议打包 //此处截取数据并进行websocket协议打包
std::weak_ptr<WebSocketSessionBase> weakSelf = std::static_pointer_cast<WebSocketSessionBase>(HttpSessionType::shared_from_this()); std::weak_ptr<WebSocketSessionBase> weakSelf = std::static_pointer_cast<WebSocketSessionBase>(HttpSessionType::shared_from_this());
std::dynamic_pointer_cast<SendInterceptor>(_session)->setOnBeforeSendCB([weakSelf](const toolkit::Buffer::Ptr &buf) { std::dynamic_pointer_cast<SendInterceptor>(_session)->setOnBeforeSendCB([weakSelf, data_type](const toolkit::Buffer::Ptr &buf) {
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
if (strongSelf) { if (strongSelf) {
mediakit::WebSocketHeader header; mediakit::WebSocketHeader header;
header._fin = true; header._fin = true;
header._reserved = 0; header._reserved = 0;
header._opcode = DataType; header._opcode = data_type;
header._mask_flag = false; header._mask_flag = false;
strongSelf->HttpSessionType::encode(header, buf); strongSelf->HttpSessionType::encode(header, buf);
} }

View File

@ -81,9 +81,11 @@ public:
*/ */
struct EchoSessionCreator { struct EchoSessionCreator {
//返回的Session必须派生于SendInterceptor可以返回null(拒绝连接) //返回的Session必须派生于SendInterceptor可以返回null(拒绝连接)
Session::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock) { Session::Ptr operator()(const Parser &header, const HttpSession &parent, const Socket::Ptr &pSock, mediakit::WebSocketHeader::Type &type) {
// return nullptr; // return nullptr;
if (header.url() == "/") { if (header.url() == "/") {
// 可以指定传输方式
// type = mediakit::WebSocketHeader::BINARY;
return std::make_shared<SessionTypeImp<EchoSession> >(header, parent, pSock); return std::make_shared<SessionTypeImp<EchoSession> >(header, parent, pSock);
} }
return std::make_shared<SessionTypeImp<EchoSessionWithUrl> >(header, parent, pSock); return std::make_shared<SessionTypeImp<EchoSessionWithUrl> >(header, parent, pSock);