Skip to content

Commit

Permalink
增加视频直播功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jxy918 committed Jan 4, 2019
1 parent 0cf017a commit 40b1d07
Show file tree
Hide file tree
Showing 28 changed files with 439 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ LOG_ENABLE=true
APP_DEBUG=true

# Server, 开启次配置如果没有安装consul会报错,如果想要使用consul功能,请自行安装consul
#AUTO_REGISTER=true
AUTO_REGISTER=true
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,21 @@ php ./test/tcp_client
```

* 如果使用swoft的Dockerfile需要自行安装msgpack扩展
* **[swoft框架](https://github.com/swoft-cloud/swoft/)**
* **[swoft框架](https://github.com/swoft-cloud/swoft/)**

### 八,增加录像直播功能, 此功能只是测试

* 1, 视频录制url,如果电脑没有摄像头, 请用手机测试录制视频:

````
http://[ip]:[port]/camera
````

* 2,浏览器上播放视频url如下:

```
http://[ip]:[port]/show
```



Empty file modified app/Boot/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Breaker/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Commands/.gitkeep
100644 → 100755
Empty file.
1 change: 0 additions & 1 deletion app/Controllers/DbproxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public function testDbproxy() {
* @return array
*/
public function testNotify() {
//查询用户信息
$result = $this->notifyService->notify('aaaaaaaaabbb');
return $result;
}
Expand Down
85 changes: 84 additions & 1 deletion app/Controllers/GameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
use App\Game\Core\Log;
use Swoft\HttpClient\Client;

/**
* Class GameController
Expand All @@ -39,6 +40,11 @@ class GameController{
*/
public $userinfo = array();

/**
* consul 发现服务url
*/
const DISCOVERY_PATH = 'http://127.0.0.1:8500/v1/health/service/%s?passing=1&dc=dc1&near';

/**
* this is a example view, test view
* @RequestMapping(route="/game", method={RequestMethod::GET})
Expand Down Expand Up @@ -104,7 +110,7 @@ private function _isLogin(Request $request)
}

/**
* this is a example view, test view
* 广播当前服务器消息
* @RequestMapping(route="/broadcast", method=RequestMethod::GET)
* @return array
*/
Expand Down Expand Up @@ -172,4 +178,81 @@ protected function pushToAll($serv, $data)
}
return $client;
}

/**
* 广播全服
* @RequestMapping(route="/broadcast_to_all", method=RequestMethod::GET)
* @return array
*/
public function broadcastToAll(Request $request)
{
$msg = $request->query('msg');
$msg = !empty($msg) ? $msg : "this is a system msg";
//走consul注册发现服务器来广播消息,获取服务器列表
$serviceList = $this->getServiceList();
$result = [];
//采用http循环发送消息
foreach($serviceList as $v) {
$notify_url = "http://{$v}/broadcast?msg={$msg}";
$httpClient = new Client();
$result[$v] = $httpClient->get($notify_url)->getResult();
}
return $result;
}

/**
* get service list, 默认就是游戏网关服务器的consul服务器name
*
* @param string $serviceName
* @return array
*/
public function getServiceList($serviceName = 'gateway')
{
$httpClient = new Client();
$url = sprintf(self::DISCOVERY_PATH, $serviceName);
$result = $httpClient->get($url)->getResult();
$services = json_decode($result, true);

// 数据格式化
$nodes = [];
foreach ($services as $service) {
if (!isset($service['Service'])) {
App::warning("consul[Service] 服务健康节点集合,数据格式不不正确,Data=" . $result);
continue;
}
$serviceInfo = $service['Service'];
if (!isset($serviceInfo['Address'], $serviceInfo['Port'])) {
App::warning("consul[Address] Or consul[Port] 服务健康节点集合,数据格式不不正确,Data=" . $result);
continue;
}
$address = $serviceInfo['Address'];
$port = $serviceInfo['Port'];

$uri = implode(":", [$address, $port]);
$nodes[] = $uri;
}
return $nodes;
}

/**
* this is a example view, test view
* @RequestMapping(route="/camera", method={RequestMethod::GET})
* @View(template="vedio/camera")
* @return array
*/
public function camera(Request $request, Response $response)
{
return [];
}

/**
* this is a example view, test view
* @RequestMapping(route="/show", method={RequestMethod::GET})
* @View(template="vedio/show")
* @return array
*/
public function show(Request $request, Response $response)
{
return [];
}
}
Empty file modified app/Fallback/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Lib/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Listener/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Middlewares/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Models/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Models/Dao/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Models/Data/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Models/Entity/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Models/Logic/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Process/.gitkeep
100644 → 100755
Empty file.
Empty file modified app/Services/.gitkeep
100644 → 100755
Empty file.
26 changes: 24 additions & 2 deletions app/Services/NotifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@
*/
class NotifyService implements NotifyInterface
{
public function notify(string $msg = '')
public function notify(string $msg = 'this is system msg')
{
$msg = 'this is system msg';
$data = Packet::packFormat('OK', 0, $msg);
$data = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::BROADCAST_MSG_RESP);
$ws_serv = App::$server->getServer();
Expand Down Expand Up @@ -66,4 +65,27 @@ protected function pushToAll($serv, $data)
}
return $client;
}

protected function notifyMsg($serv, $data)
{
$client = array();
$start_fd = 0;
while(true) {
$conn_list = $serv->getClientList($start_fd, 10);
if ($conn_list===false or count($conn_list) === 0) {
Log::show('BroadCast finish');
break;
}
$start_fd = end($conn_list);
foreach($conn_list as $fd) {
//获取客户端信息
$client_info = $serv->getClientInfo($fd);
$client[$fd] = $client_info;
if(isset($client_info['websocket_status']) && $client_info['websocket_status'] == 3) {
$serv->push($fd, $data, WEBSOCKET_OPCODE_BINARY);
}
}
}
return $client;
}
}
Empty file modified app/Tasks/.gitkeep
100644 → 100755
Empty file.
123 changes: 123 additions & 0 deletions app/WebSocket/VedioController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://doc.swoft.org
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/

namespace App\WebSocket;

use Swoft\Http\Message\Server\Request;
use Swoft\Http\Message\Server\Response;
use Swoft\WebSocket\Server\Bean\Annotation\WebSocket;
use Swoft\WebSocket\Server\HandlerInterface;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
use App\Middlewares\GameMiddleware;

/**
* Class VedioController - This is an controller for handle websocket
* @package App\WebSocket
* @WebSocket("/vedio")
*/
class VedioController implements HandlerInterface
{
/**
* 在这里你可以验证握手的请求信息
* - 必须返回含有两个元素的array
* - 第一个元素的值来决定是否进行握手
* - 第二个元素是response对象
* - 可以在response设置一些自定义header,body等信息
* @param Request $request
* @param Response $response
* @return array
* [
* self::HANDSHAKE_OK,
* $response
* ]
*/
public function checkHandshake(Request $request, Response $response): array
{
// some validate logic ...
return [self::HANDSHAKE_OK, $response];
}

/**
*
* @param Server $server
* @param Request $request
* @param int $fd
* @return mixed
*/
public function onOpen(Server $server, Request $request, int $fd)
{
echo "vedio connnected #{$fd}...";

}

/**
* @param Server $server
* @param Frame $frame
* @return mixed
*/
public function onMessage(Server $server, Frame $frame)
{
//广播消息
echo "vedio message #{$frame->fd}...data:".$frame->data."\n";
if(!is_numeric($frame->data)) {
//如果是录像数据, 发送二进制数据
$this->pushToAll($server, $frame->data, WEBSOCKET_OPCODE_BINARY);
}
}

/**
* @param Server $server
* @param int $fd
* @return mixed
*/
public function onRequest(Request $request, Response $response)
{
echo 'vedio request ...';
}

/**
* @param Server $server
* @param int $fd
* @return mixed
*/
public function onClose(Server $server, int $fd)
{
echo "vedio close #{$fd}...";
}

/**
* 当connetions属性无效时可以使用此方法,服务器广播消息, 此方法是给所有的连接客户端, 广播消息,通过方法getClientList广播
* @param $serv
* @param $data
*/
protected function pushToAll($serv, $data)
{
$client = array();
$start_fd = 0;
while(true) {
$conn_list = $serv->getClientList($start_fd, 10);
if ($conn_list===false or count($conn_list) === 0) {
Log::show('BroadCast finish');
break;
}
$start_fd = end($conn_list);
foreach($conn_list as $fd) {
//获取客户端信息
$client_info = $serv->getClientInfo($fd);
$client[$fd] = $client_info;
if(isset($client_info['websocket_status']) && $client_info['websocket_status'] == 3) {
$serv->push($fd, $data);
}
}
}
//return $client;
}
}
2 changes: 1 addition & 1 deletion config/properties/service.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'name' => 'notify',
'uri' => [
'192.168.7.197:20000',
'192.168.7.197:20000',
'192.168.7.197:30000',
],
'minActive' => 8,
'maxActive' => 8,
Expand Down
6 changes: 3 additions & 3 deletions config/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//PS:扩展游戏服务器, 注意此tcp服务器需要自己去创建实现
'tcp1' => [
'host' => env('TCP_HOST', '0.0.0.0'),
'port' => env('TCP_PORT', 20001),
'port' => env('TCP_PORT', 20002),
'type' => env('TCP_TYPE', SWOOLE_SOCK_TCP),
'package_max_length' => env('TCP_PACKAGE_MAX_LENGTH', 2048),
'open_eof_check' => env('TCP_OPEN_EOF_CHECK', false),
Expand Down Expand Up @@ -58,8 +58,8 @@
'daemonize' => env('DAEMONIZE', 0),
'dispatch_mode' => env('DISPATCH_MODE', 2),
'log_file' => env('LOG_FILE', '@runtime/logs/swoole.log'),
'task_worker_num' => env('TASK_WORKER_NUM', 1),
'package_max_length' => env('PACKAGE_MAX_LENGTH', 2048),
'task_worker_num' => env('TASK_WORKER_NUM', 2),
'package_max_length' => env('PACKAGE_MAX_LENGTH', 20480),
'upload_tmp_dir' => env('UPLOAD_TMP_DIR', '@runtime/uploadfiles'),
'document_root' => env('DOCUMENT_ROOT', BASE_PATH . '/public'),
'enable_static_handler' => env('ENABLE_STATIC_HANDLER', true),
Expand Down
Loading

0 comments on commit 40b1d07

Please sign in to comment.