From f53879cff19a162009ff4fcc85163846eac56d7f Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Wed, 26 Sep 2018 23:27:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E5=88=B0=E7=A1=AE=E5=88=87=E7=9A=84We?= =?UTF-8?q?bSocket=E6=95=B0=E6=8D=AE=E6=97=B6=E6=89=8D=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E4=B8=9A=E5=8A=A1=E9=80=BB=E8=BE=91=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpSession.h | 54 +++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/src/Http/HttpSession.h b/src/Http/HttpSession.h index efc7df13..f9a54d4c 100644 --- a/src/Http/HttpSession.h +++ b/src/Http/HttpSession.h @@ -135,43 +135,22 @@ private: template class WebSocketSession : public HttpSession { public: - WebSocketSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : HttpSession(pTh,pSock){ - _session = std::make_shared(pTh,pSock); - } - virtual ~WebSocketSession(){}; + WebSocketSession(const std::shared_ptr &pTh, const Socket::Ptr &pSock) : HttpSession(pTh,pSock){} + virtual ~WebSocketSession(){} //收到eof或其他导致脱离TcpServer事件的回调 void onError(const SockException &err) override{ HttpSession::onError(err); - _session->onError(err); + if(_session){ + _session->onError(err); + } } //每隔一段时间触发,用来做超时管理 void onManager() override{ HttpSession::onManager(); - _session->onManager(); - } - //在创建TcpSession后,TcpServer会把自身的配置参数通过该函数传递给TcpSession - void attachServer(const TcpServer &server) override{ - HttpSession::attachServer(server); - _session->attachServer(server); - - //此处截取数据并进行websocket协议打包 - weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); - _session->setOnBeforeSendCB([weakSelf](const Buffer::Ptr &buf){ - auto strongSelf = weakSelf.lock(); - if(strongSelf){ - bool mask_flag = strongSelf->_mask_flag; - strongSelf->_mask_flag = false; - strongSelf->WebSocketSplitter::encode((uint8_t *)buf->data(),buf->size()); - strongSelf->_mask_flag = mask_flag; - } - return buf->size(); - }); - - } - //作为该TcpSession的唯一标识符 - string getIdentifier() const override{ - return _session->getIdentifier(); + if(_session){ + _session->onManager(); + } } protected: /** @@ -181,6 +160,22 @@ protected: void onWebSocketDecodeHeader(const WebSocketHeader &packet) override{ //新包,原来的包残余数据清空掉 _remian_data.clear(); + if(_firstPacket){ + _firstPacket = false; + _session = std::make_shared(nullptr,_sock); + //此处截取数据并进行websocket协议打包 + weak_ptr weakSelf = dynamic_pointer_cast(shared_from_this()); + _session->setOnBeforeSendCB([weakSelf](const Buffer::Ptr &buf){ + auto strongSelf = weakSelf.lock(); + if(strongSelf){ + bool mask_flag = strongSelf->_mask_flag; + strongSelf->_mask_flag = false; + strongSelf->WebSocketSplitter::encode((uint8_t *)buf->data(),buf->size()); + strongSelf->_mask_flag = mask_flag; + } + return buf->size(); + }); + } } /** @@ -280,6 +275,7 @@ private: private: std::shared_ptr _session; string _remian_data; + bool _firstPacket = true; }; /**