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
a871fc18
Commit
a871fc18
authored
a year ago
by
xia-chu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
websocket服务器支持在创建器中指定消息类型
通过该修改可以更灵活的指定同端口下不同websocket服务的消息类型
parent
ebde21a3
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
8 行增加
和
5 行删除
+8
-5
src/Http/WebSocketSession.h
+5
-4
tests/test_wsServer.cpp
+3
-1
没有找到文件。
src/Http/WebSocketSession.h
查看文件 @
a871fc18
...
@@ -69,7 +69,7 @@ template <typename SessionType>
...
@@ -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:
...
@@ -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:
...
@@ -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
=
DataT
ype
;
header
.
_opcode
=
data_t
ype
;
header
.
_mask_flag
=
false
;
header
.
_mask_flag
=
false
;
strongSelf
->
HttpSessionType
::
encode
(
header
,
buf
);
strongSelf
->
HttpSessionType
::
encode
(
header
,
buf
);
}
}
...
...
This diff is collapsed.
Click to expand it.
tests/test_wsServer.cpp
查看文件 @
a871fc18
...
@@ -81,9 +81,11 @@ public:
...
@@ -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
);
...
...
This diff is collapsed.
Click to expand it.
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论