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
ee8d28db
Commit
ee8d28db
authored
Sep 17, 2019
by
xiongziliang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
单独提供websocket服务器程序
parent
4d84ae92
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
97 行增加
和
43 行删除
+97
-43
server/main.cpp
+3
-3
src/Http/WebSocketSession.h
+0
-32
tests/test_httpApi.cpp
+2
-2
tests/test_server.cpp
+6
-6
tests/test_wsServer.cpp
+86
-0
没有找到文件。
server/main.cpp
查看文件 @
ee8d28db
...
...
@@ -270,13 +270,13 @@ int main(int argc,char *argv[]) {
shellSrv
->
start
<
ShellSession
>
(
shellPort
);
rtspSrv
->
start
<
RtspSession
>
(
rtspPort
);
//默认554
rtmpSrv
->
start
<
RtmpSession
>
(
rtmpPort
);
//默认1935
//http服务器
,支持websocket
httpSrv
->
start
<
EchoWebSocket
Session
>
(
httpPort
);
//默认80
//http服务器
httpSrv
->
start
<
Http
Session
>
(
httpPort
);
//默认80
//如果支持ssl,还可以开启https服务器
TcpServer
::
Ptr
httpsSrv
(
new
TcpServer
());
//https服务器,支持websocket
httpsSrv
->
start
<
SSLEchoWebSocket
Session
>
(
httpsPort
);
//默认443
httpsSrv
->
start
<
Https
Session
>
(
httpsPort
);
//默认443
//支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
TcpServer
::
Ptr
rtspSSLSrv
(
new
TcpServer
());
...
...
src/Http/WebSocketSession.h
查看文件 @
ee8d28db
...
...
@@ -199,37 +199,5 @@ private:
std
::
shared_ptr
<
SessionImp
>
_session
;
};
/**
* 回显会话
*/
class
EchoSession
:
public
TcpSession
{
public
:
EchoSession
(
const
Socket
::
Ptr
&
pSock
)
:
TcpSession
(
pSock
){
DebugL
;
}
virtual
~
EchoSession
(){
DebugL
;
}
void
attachServer
(
const
TcpServer
&
server
)
override
{
DebugL
<<
getIdentifier
()
<<
" "
<<
TcpSession
::
getIdentifier
();
}
void
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
override
{
//回显数据
send
(
buffer
);
}
void
onError
(
const
SockException
&
err
)
override
{
WarnL
<<
err
.
what
();
}
//每隔一段时间触发,用来做超时管理
void
onManager
()
override
{
DebugL
;
}
};
typedef
WebSocketSession
<
EchoSession
,
HttpSession
>
EchoWebSocketSession
;
typedef
WebSocketSession
<
EchoSession
,
HttpsSession
>
SSLEchoWebSocketSession
;
#endif //ZLMEDIAKIT_WEBSOCKETSESSION_H
tests/test_httpApi.cpp
查看文件 @
ee8d28db
...
...
@@ -124,11 +124,11 @@ int main(int argc,char *argv[]){
//开启http服务器
TcpServer
::
Ptr
httpSrv
(
new
TcpServer
());
httpSrv
->
start
<
EchoWebSocket
Session
>
(
mINI
::
Instance
()[
Http
::
kPort
]);
//默认80
httpSrv
->
start
<
Http
Session
>
(
mINI
::
Instance
()[
Http
::
kPort
]);
//默认80
//如果支持ssl,还可以开启https服务器
TcpServer
::
Ptr
httpsSrv
(
new
TcpServer
());
httpsSrv
->
start
<
SSLEchoWebSocket
Session
>
(
mINI
::
Instance
()[
Http
::
kSSLPort
]);
//默认443
httpsSrv
->
start
<
Https
Session
>
(
mINI
::
Instance
()[
Http
::
kSSLPort
]);
//默认443
InfoL
<<
"你可以在浏览器输入:http://127.0.0.1/api/my_api?key0=val0&key1=参数1"
<<
endl
;
...
...
tests/test_server.cpp
查看文件 @
ee8d28db
...
...
@@ -300,13 +300,13 @@ int main(int argc,char *argv[]) {
shellSrv
->
start
<
ShellSession
>
(
shellPort
);
rtspSrv
->
start
<
RtspSession
>
(
rtspPort
);
//默认554
rtmpSrv
->
start
<
RtmpSession
>
(
rtmpPort
);
//默认1935
//http服务器
,支持websocket
httpSrv
->
start
<
EchoWebSocket
Session
>
(
httpPort
);
//默认80
//http服务器
httpSrv
->
start
<
Http
Session
>
(
httpPort
);
//默认80
//如果支持ssl,还可以开启https服务器
TcpServer
::
Ptr
httpsSrv
(
new
TcpServer
());
//https服务器
,支持websocket
httpsSrv
->
start
<
SSLEchoWebSocket
Session
>
(
httpsPort
);
//默认443
//https服务器
httpsSrv
->
start
<
Https
Session
>
(
httpsPort
);
//默认443
//支持ssl加密的rtsp服务器,可用于诸如亚马逊echo show这样的设备访问
TcpServer
::
Ptr
rtspSSLSrv
(
new
TcpServer
());
...
...
@@ -332,12 +332,12 @@ int main(int argc,char *argv[]) {
}
if
(
httpPort
!=
mINI
::
Instance
()[
Http
::
kPort
].
as
<
uint16_t
>
()){
httpPort
=
mINI
::
Instance
()[
Http
::
kPort
];
httpSrv
->
start
<
EchoWebSocket
Session
>
(
httpPort
);
httpSrv
->
start
<
Http
Session
>
(
httpPort
);
InfoL
<<
"重启http服务器"
<<
httpPort
;
}
if
(
httpsPort
!=
mINI
::
Instance
()[
Http
::
kSSLPort
].
as
<
uint16_t
>
()){
httpsPort
=
mINI
::
Instance
()[
Http
::
kSSLPort
];
httpsSrv
->
start
<
SSLEchoWebSocket
Session
>
(
httpsPort
);
httpsSrv
->
start
<
Https
Session
>
(
httpsPort
);
InfoL
<<
"重启https服务器"
<<
httpsPort
;
}
...
...
tests/test_wsServer.cpp
0 → 100644
查看文件 @
ee8d28db
/*
* MIT License
*
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <signal.h>
#include <string>
#include <iostream>
#include "Util/MD5.h"
#include "Util/logger.h"
#include "Http/WebSocketSession.h"
using
namespace
std
;
using
namespace
toolkit
;
using
namespace
mediakit
;
/**
* 回显会话
*/
class
EchoSession
:
public
TcpSession
{
public
:
EchoSession
(
const
Socket
::
Ptr
&
pSock
)
:
TcpSession
(
pSock
){
DebugL
;
}
virtual
~
EchoSession
(){
DebugL
;
}
void
attachServer
(
const
TcpServer
&
server
)
override
{
DebugL
<<
getIdentifier
()
<<
" "
<<
TcpSession
::
getIdentifier
();
}
void
onRecv
(
const
Buffer
::
Ptr
&
buffer
)
override
{
//回显数据
send
(
buffer
);
}
void
onError
(
const
SockException
&
err
)
override
{
WarnL
<<
err
.
what
();
}
//每隔一段时间触发,用来做超时管理
void
onManager
()
override
{
DebugL
;
}
};
int
main
(
int
argc
,
char
*
argv
[])
{
//设置日志
Logger
::
Instance
().
add
(
std
::
make_shared
<
ConsoleChannel
>
());
Logger
::
Instance
().
setWriter
(
std
::
make_shared
<
AsyncLogWriter
>
());
SSL_Initor
::
Instance
().
loadCertificate
((
exeDir
()
+
"ssl.p12"
).
data
());
TcpServer
::
Ptr
httpSrv
(
new
TcpServer
());
//http服务器,支持websocket
httpSrv
->
start
<
WebSocketSession
<
EchoSession
,
HttpSession
>>
(
80
);
//默认80
TcpServer
::
Ptr
httpsSrv
(
new
TcpServer
());
//https服务器,支持websocket
httpsSrv
->
start
<
WebSocketSession
<
EchoSession
,
HttpsSession
>>
(
443
);
//默认443
//设置退出信号处理函数
static
semaphore
sem
;
signal
(
SIGINT
,
[](
int
)
{
sem
.
post
();
});
// 设置退出信号
sem
.
wait
();
return
0
;
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论