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
caa870c3
Commit
caa870c3
authored
Jan 15, 2020
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
websocket服务器支持根据url选择不同的运行逻辑
parent
34e3e9f7
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
139 行增加
和
53 行删除
+139
-53
src/Http/WebSocketSession.h
+87
-50
tests/test_wsServer.cpp
+52
-3
没有找到文件。
src/Http/WebSocketSession.h
查看文件 @
caa870c3
...
...
@@ -31,15 +31,79 @@
#include "Network/TcpServer.h"
/**
* 数据发送拦截器
*/
class
SendInterceptor
{
public
:
typedef
function
<
int
(
const
Buffer
::
Ptr
&
buf
)
>
onBeforeSendCB
;
SendInterceptor
()
=
default
;
virtual
~
SendInterceptor
()
=
default
;
virtual
void
setOnBeforeSendCB
(
const
onBeforeSendCB
&
cb
)
=
0
;
};
/**
* 该类实现了TcpSession派生类发送数据的截取
* 目的是发送业务数据前进行websocket协议的打包
*/
template
<
typename
TcpSessionType
>
class
TcpSessionTypeImp
:
public
TcpSessionType
,
public
SendInterceptor
{
public
:
typedef
std
::
shared_ptr
<
TcpSessionTypeImp
>
Ptr
;
TcpSessionTypeImp
(
const
Parser
&
header
,
const
HttpSession
&
parent
,
const
Socket
::
Ptr
&
pSock
)
:
_identifier
(
parent
.
getIdentifier
()),
TcpSessionType
(
pSock
)
{}
~
TcpSessionTypeImp
()
{}
/**
* 设置发送数据截取回调函数
* @param cb 截取回调函数
*/
void
setOnBeforeSendCB
(
const
onBeforeSendCB
&
cb
)
override
{
_beforeSendCB
=
cb
;
}
protected
:
/**
* 重载send函数截取数据
* @param buf 需要截取的数据
* @return 数据字节数
*/
int
send
(
const
Buffer
::
Ptr
&
buf
)
override
{
if
(
_beforeSendCB
)
{
return
_beforeSendCB
(
buf
);
}
return
TcpSessionType
::
send
(
buf
);
}
string
getIdentifier
()
const
override
{
return
_identifier
;
}
private
:
onBeforeSendCB
_beforeSendCB
;
string
_identifier
;
};
template
<
typename
TcpSessionType
>
class
TcpSessionCreator
{
public
:
//返回的TcpSession必须派生于SendInterceptor,可以返回null
TcpSession
::
Ptr
operator
()(
const
Parser
&
header
,
const
HttpSession
&
parent
,
const
Socket
::
Ptr
&
pSock
){
return
std
::
make_shared
<
TcpSessionTypeImp
<
TcpSessionType
>
>
(
header
,
parent
,
pSock
);
}
};
/**
* 通过该模板类可以透明化WebSocket协议,
* 用户只要实现WebSock协议下的具体业务协议,譬如基于WebSocket协议的Rtmp协议等
* @tparam SessionType 业务协议的TcpSession类
*/
template
<
class
SessionType
,
class
HttpSessionType
=
HttpSession
,
WebSocketHeader
::
Type
DataType
=
WebSocketHeader
::
TEXT
>
class
WebSocketSession
:
public
HttpSessionType
{
template
<
typename
Creator
,
typename
HttpSessionType
=
HttpSession
,
WebSocketHeader
::
Type
DataType
=
WebSocketHeader
::
TEXT
>
class
WebSocketSession
Base
:
public
HttpSessionType
{
public
:
WebSocketSession
(
const
Socket
::
Ptr
&
pSock
)
:
HttpSessionType
(
pSock
){}
virtual
~
WebSocketSession
(){}
WebSocketSession
Base
(
const
Socket
::
Ptr
&
pSock
)
:
HttpSessionType
(
pSock
){}
virtual
~
WebSocketSession
Base
(){}
//收到eof或其他导致脱离TcpServer事件的回调
void
onError
(
const
SockException
&
err
)
override
{
...
...
@@ -69,23 +133,27 @@ protected:
*/
bool
onWebSocketConnect
(
const
Parser
&
header
)
override
{
//创建websocket session类
_session
=
std
::
make_shared
<
SessionImp
>
(
HttpSessionType
::
getIdentifier
(),
HttpSessionType
::
_sock
);
_session
=
_creator
(
header
,
*
this
,
HttpSessionType
::
_sock
);
if
(
!
_session
){
//此url不允许创建websocket连接
return
false
;
}
auto
strongServer
=
_weakServer
.
lock
();
if
(
strongServer
){
_session
->
attachServer
(
*
strongServer
);
}
//此处截取数据并进行websocket协议打包
weak_ptr
<
WebSocketSession
>
weakSelf
=
dynamic_pointer_cast
<
WebSocketSession
>
(
HttpSessionType
::
shared_from_this
());
_session
->
setOnBeforeSendCB
([
weakSelf
](
const
Buffer
::
Ptr
&
buf
)
{
weak_ptr
<
WebSocketSession
Base
>
weakSelf
=
dynamic_pointer_cast
<
WebSocketSessionBase
>
(
HttpSessionType
::
shared_from_this
());
dynamic_pointer_cast
<
SendInterceptor
>
(
_session
)
->
setOnBeforeSendCB
([
weakSelf
](
const
Buffer
::
Ptr
&
buf
)
{
auto
strongSelf
=
weakSelf
.
lock
();
if
(
strongSelf
)
{
if
(
strongSelf
)
{
WebSocketHeader
header
;
header
.
_fin
=
true
;
header
.
_reserved
=
0
;
header
.
_opcode
=
DataType
;
header
.
_mask_flag
=
false
;
strongSelf
->
WebSocketSplitter
::
encode
(
header
,
buf
);
strongSelf
->
WebSocketSplitter
::
encode
(
header
,
buf
);
}
return
buf
->
size
();
});
...
...
@@ -156,49 +224,18 @@ protected:
SocketHelper
::
send
(
buffer
);
}
private
:
typedef
function
<
int
(
const
Buffer
::
Ptr
&
buf
)
>
onBeforeSendCB
;
/**
* 该类实现了TcpSession派生类发送数据的截取
* 目的是发送业务数据前进行websocket协议的打包
*/
class
SessionImp
:
public
SessionType
{
public
:
SessionImp
(
const
string
&
identifier
,
const
Socket
::
Ptr
&
pSock
)
:
_identifier
(
identifier
),
SessionType
(
pSock
){}
~
SessionImp
(){}
/**
* 设置发送数据截取回调函数
* @param cb 截取回调函数
*/
void
setOnBeforeSendCB
(
const
onBeforeSendCB
&
cb
){
_beforeSendCB
=
cb
;
}
protected
:
/**
* 重载send函数截取数据
* @param buf 需要截取的数据
* @return 数据字节数
*/
int
send
(
const
Buffer
::
Ptr
&
buf
)
override
{
if
(
_beforeSendCB
){
return
_beforeSendCB
(
buf
);
}
return
SessionType
::
send
(
buf
);
}
string
getIdentifier
()
const
override
{
return
_identifier
;
}
private
:
onBeforeSendCB
_beforeSendCB
;
string
_identifier
;
};
private
:
string
_remian_data
;
weak_ptr
<
TcpServer
>
_weakServer
;
std
::
shared_ptr
<
SessionImp
>
_session
;
TcpSession
::
Ptr
_session
;
Creator
_creator
;
};
template
<
typename
TcpSessionType
,
typename
HttpSessionType
=
HttpSession
,
WebSocketHeader
::
Type
DataType
=
WebSocketHeader
::
TEXT
>
class
WebSocketSession
:
public
WebSocketSessionBase
<
TcpSessionCreator
<
TcpSessionType
>
,
HttpSessionType
,
DataType
>
{
public
:
WebSocketSession
(
const
Socket
::
Ptr
&
pSock
)
:
WebSocketSessionBase
<
TcpSessionCreator
<
TcpSessionType
>
,
HttpSessionType
,
DataType
>
(
pSock
){}
virtual
~
WebSocketSession
(){}
};
#endif //ZLMEDIAKIT_WEBSOCKETSESSION_H
tests/test_wsServer.cpp
查看文件 @
caa870c3
...
...
@@ -51,6 +51,7 @@ public:
}
void
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
override
{
//回显数据
send
(
"from EchoSession:"
);
send
(
buffer
);
}
void
onError
(
const
SockException
&
err
)
override
{
...
...
@@ -62,6 +63,48 @@ public:
}
};
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
);
}
};
int
main
(
int
argc
,
char
*
argv
[])
{
//设置日志
Logger
::
Instance
().
add
(
std
::
make_shared
<
ConsoleChannel
>
());
...
...
@@ -71,13 +114,19 @@ int main(int argc, char *argv[]) {
TcpServer
::
Ptr
httpSrv
(
new
TcpServer
());
//http服务器,支持websocket
httpSrv
->
start
<
WebSocketSession
<
EchoSession
,
HttpSession
>
>
(
80
);
//默认80
httpSrv
->
start
<
WebSocketSession
Base
<
EchoSessionCreator
,
HttpSession
>
>
(
80
);
//默认80
TcpServer
::
Ptr
httpsSrv
(
new
TcpServer
());
//https服务器,支持websocket
httpsSrv
->
start
<
WebSocketSession
<
EchoSession
,
HttpsSession
>>
(
443
);
//默认443
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选择不同的处理逻辑"
;
DebugL
<<
"请打开网页:http://www.websocket-test.com/,连接 ws://127.0.0.1/测试"
;
//设置退出信号处理函数
static
semaphore
sem
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论