Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Z
ZLMediaKit
概览
Overview
Details
Activity
Cycle Analytics
版本库
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
问题
0
Issues
0
列表
Board
标记
里程碑
合并请求
0
Merge Requests
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
Snippets
成员
Collapse sidebar
Close sidebar
活动
图像
聊天
创建新问题
作业
提交
Issue Boards
Open sidebar
张翔宇
ZLMediaKit
Commits
756ec136
Commit
756ec136
authored
Jan 25, 2022
by
ziyue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
websocket客户端触发事件时确保对象强引用有效
同时修复websocket客户端判断alive返回false的bug
parent
4f278943
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
27 行增加
和
13 行删除
+27
-13
3rdpart/ZLToolKit
+1
-1
src/Http/WebSocketClient.h
+26
-12
没有找到文件。
ZLToolKit
@
25062620
Subproject commit
6e5f08d89cd6fd06a4185f5de937ef629c24a83
e
Subproject commit
25062620233c62475aaffc0a9960e2689d8418c
e
src/Http/WebSocketClient.h
查看文件 @
756ec136
...
...
@@ -72,9 +72,10 @@ class HttpWsClient : public HttpClientImp , public WebSocketSplitter{
public
:
typedef
shared_ptr
<
HttpWsClient
>
Ptr
;
HttpWsClient
(
ClientTypeImp
<
ClientType
,
DataType
>
&
delegate
)
:
_delegate
(
delegate
){
HttpWsClient
(
const
std
::
shared_ptr
<
ClientTypeImp
<
ClientType
,
DataType
>
>
&
delegate
)
:
_weak_delegate
(
delegate
),
_delegate
(
*
delegate
)
{
_Sec_WebSocket_Key
=
encodeBase64
(
makeRandStr
(
16
,
false
));
setPoller
(
delegate
.
getPoller
());
setPoller
(
_
delegate
.
getPoller
());
}
~
HttpWsClient
(){}
...
...
@@ -153,14 +154,20 @@ protected:
//TcpClient override
void
onRecv
(
const
Buffer
::
Ptr
&
buf
)
override
{
auto
strong_ref
=
_weak_delegate
.
lock
();;
HttpClientImp
::
onRecv
(
buf
);
}
/**
* 定时触发
*/
void
onManager
()
override
{
if
(
_onRecv
){
auto
strong_ref
=
_weak_delegate
.
lock
();;
if
(
_onRecv
)
{
//websocket连接成功了
_delegate
.
onManager
();
}
else
{
}
else
{
//websocket连接中...
HttpClientImp
::
onManager
();
}
...
...
@@ -169,11 +176,12 @@ protected:
/**
* 数据全部发送完毕后回调
*/
void
onFlush
()
override
{
if
(
_onRecv
){
void
onFlush
()
override
{
auto
strong_ref
=
_weak_delegate
.
lock
();;
if
(
_onRecv
)
{
//websocket连接成功了
_delegate
.
onFlush
();
}
else
{
}
else
{
//websocket连接中...
HttpClientImp
::
onFlush
();
}
...
...
@@ -182,8 +190,9 @@ protected:
/**
* tcp连接结果
*/
void
onConnect
(
const
SockException
&
ex
)
override
{
if
(
ex
){
void
onConnect
(
const
SockException
&
ex
)
override
{
auto
strong_ref
=
_weak_delegate
.
lock
();;
if
(
ex
)
{
//tcp连接失败,直接返回失败
onWebSocketException
(
ex
);
return
;
...
...
@@ -195,7 +204,8 @@ protected:
/**
* tcp连接断开
*/
void
onErr
(
const
SockException
&
ex
)
override
{
void
onErr
(
const
SockException
&
ex
)
override
{
auto
strong_ref
=
_weak_delegate
.
lock
();;
//tcp断开或者shutdown导致的断开
onWebSocketException
(
ex
);
}
...
...
@@ -335,6 +345,7 @@ private:
private
:
string
_Sec_WebSocket_Key
;
function
<
void
(
const
char
*
data
,
size_t
len
)
>
_onRecv
;
weak_ptr
<
ClientTypeImp
<
ClientType
,
DataType
>
>
_weak_delegate
;
ClientTypeImp
<
ClientType
,
DataType
>
&
_delegate
;
string
_payload_section
;
string
_payload_cache
;
...
...
@@ -354,7 +365,6 @@ public:
template
<
typename
...
ArgsType
>
WebSocketClient
(
ArgsType
&&
...
args
)
:
ClientTypeImp
<
ClientType
,
DataType
>
(
std
::
forward
<
ArgsType
>
(
args
)...){
_wsClient
.
reset
(
new
HttpWsClient
<
ClientType
,
DataType
>
(
*
this
));
}
~
WebSocketClient
()
override
{
_wsClient
->
closeWsClient
();
...
...
@@ -377,10 +387,14 @@ public:
//明文ws
ws_url
=
StrPrinter
<<
"ws://"
+
host
<<
":"
<<
port
<<
"/"
;
}
_wsClient
->
startWsClien
t
(
ws_url
,
timeout_sec
);
startWebSocke
t
(
ws_url
,
timeout_sec
);
}
void
startWebSocket
(
const
string
&
ws_url
,
float
fTimeOutSec
=
3
){
_wsClient
=
std
::
make_shared
<
HttpWsClient
<
ClientType
,
DataType
>
>
(
static_pointer_cast
<
WebSocketClient
>
(
this
->
shared_from_this
()));
_wsClient
->
setOnCreateSocket
([
this
](
const
EventPoller
::
Ptr
&
){
return
this
->
createSocket
();
});
_wsClient
->
startWsClient
(
ws_url
,
fTimeOutSec
);
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论