From 40b1d07d97691d47598c3f2a1f0002983b2a7f34 Mon Sep 17 00:00:00 2001 From: jxy918 Date: Fri, 4 Jan 2019 14:43:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=A7=86=E9=A2=91=E7=9B=B4?= =?UTF-8?q?=E6=92=AD=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 2 +- README.md | 18 +++- app/Boot/.gitkeep | 0 app/Breaker/.gitkeep | 0 app/Commands/.gitkeep | 0 app/Controllers/DbproxyController.php | 1 - app/Controllers/GameController.php | 85 +++++++++++++++++- app/Fallback/.gitkeep | 0 app/Lib/.gitkeep | 0 app/Listener/.gitkeep | 0 app/Middlewares/.gitkeep | 0 app/Models/.gitkeep | 0 app/Models/Dao/.gitkeep | 0 app/Models/Data/.gitkeep | 0 app/Models/Entity/.gitkeep | 0 app/Models/Logic/.gitkeep | 0 app/Process/.gitkeep | 0 app/Services/.gitkeep | 0 app/Services/NotifyService.php | 26 +++++- app/Tasks/.gitkeep | 0 app/WebSocket/VedioController.php | 123 ++++++++++++++++++++++++++ config/properties/service.php | 2 +- config/server.php | 6 +- resources/views/vedio/camera.php | 107 ++++++++++++++++++++++ resources/views/vedio/show.php | 79 +++++++++++++++++ runtime/logs/.gitkeep | 0 runtime/sessions/.gitkeep | 0 runtime/uploadfiles/.gitkeep | 0 28 files changed, 439 insertions(+), 10 deletions(-) mode change 100644 => 100755 app/Boot/.gitkeep mode change 100644 => 100755 app/Breaker/.gitkeep mode change 100644 => 100755 app/Commands/.gitkeep mode change 100644 => 100755 app/Fallback/.gitkeep mode change 100644 => 100755 app/Lib/.gitkeep mode change 100644 => 100755 app/Listener/.gitkeep mode change 100644 => 100755 app/Middlewares/.gitkeep mode change 100644 => 100755 app/Models/.gitkeep mode change 100644 => 100755 app/Models/Dao/.gitkeep mode change 100644 => 100755 app/Models/Data/.gitkeep mode change 100644 => 100755 app/Models/Entity/.gitkeep mode change 100644 => 100755 app/Models/Logic/.gitkeep mode change 100644 => 100755 app/Process/.gitkeep mode change 100644 => 100755 app/Services/.gitkeep mode change 100644 => 100755 app/Tasks/.gitkeep create mode 100755 app/WebSocket/VedioController.php create mode 100755 resources/views/vedio/camera.php create mode 100755 resources/views/vedio/show.php mode change 100644 => 100755 runtime/logs/.gitkeep mode change 100644 => 100755 runtime/sessions/.gitkeep mode change 100644 => 100755 runtime/uploadfiles/.gitkeep diff --git a/.env b/.env index e7004bc..b6eda20 100755 --- a/.env +++ b/.env @@ -4,4 +4,4 @@ LOG_ENABLE=true APP_DEBUG=true # Server, 开启次配置如果没有安装consul会报错,如果想要使用consul功能,请自行安装consul -#AUTO_REGISTER=true \ No newline at end of file +AUTO_REGISTER=true \ No newline at end of file diff --git a/README.md b/README.md index 803b434..3088ddb 100755 --- a/README.md +++ b/README.md @@ -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 +``` + + diff --git a/app/Boot/.gitkeep b/app/Boot/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Breaker/.gitkeep b/app/Breaker/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Commands/.gitkeep b/app/Commands/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Controllers/DbproxyController.php b/app/Controllers/DbproxyController.php index 82f5d4d..ed4b82b 100755 --- a/app/Controllers/DbproxyController.php +++ b/app/Controllers/DbproxyController.php @@ -122,7 +122,6 @@ public function testDbproxy() { * @return array */ public function testNotify() { - //查询用户信息 $result = $this->notifyService->notify('aaaaaaaaabbb'); return $result; } diff --git a/app/Controllers/GameController.php b/app/Controllers/GameController.php index a669d35..a2a3d5a 100755 --- a/app/Controllers/GameController.php +++ b/app/Controllers/GameController.php @@ -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 @@ -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}) @@ -104,7 +110,7 @@ private function _isLogin(Request $request) } /** - * this is a example view, test view + * 广播当前服务器消息 * @RequestMapping(route="/broadcast", method=RequestMethod::GET) * @return array */ @@ -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 []; + } } \ No newline at end of file diff --git a/app/Fallback/.gitkeep b/app/Fallback/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Lib/.gitkeep b/app/Lib/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Listener/.gitkeep b/app/Listener/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Middlewares/.gitkeep b/app/Middlewares/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Models/.gitkeep b/app/Models/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Models/Dao/.gitkeep b/app/Models/Dao/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Models/Data/.gitkeep b/app/Models/Data/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Models/Entity/.gitkeep b/app/Models/Entity/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Models/Logic/.gitkeep b/app/Models/Logic/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Process/.gitkeep b/app/Process/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Services/.gitkeep b/app/Services/.gitkeep old mode 100644 new mode 100755 diff --git a/app/Services/NotifyService.php b/app/Services/NotifyService.php index 3c4cda2..31afce5 100755 --- a/app/Services/NotifyService.php +++ b/app/Services/NotifyService.php @@ -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(); @@ -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; + } } \ No newline at end of file diff --git a/app/Tasks/.gitkeep b/app/Tasks/.gitkeep old mode 100644 new mode 100755 diff --git a/app/WebSocket/VedioController.php b/app/WebSocket/VedioController.php new file mode 100755 index 0000000..0e9d259 --- /dev/null +++ b/app/WebSocket/VedioController.php @@ -0,0 +1,123 @@ +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; + } +} diff --git a/config/properties/service.php b/config/properties/service.php index 4fb8439..4a3ec9e 100755 --- a/config/properties/service.php +++ b/config/properties/service.php @@ -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, diff --git a/config/server.php b/config/server.php index fd799e4..b88a8e5 100755 --- a/config/server.php +++ b/config/server.php @@ -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), @@ -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), diff --git a/resources/views/vedio/camera.php b/resources/views/vedio/camera.php new file mode 100755 index 0000000..5a637c7 --- /dev/null +++ b/resources/views/vedio/camera.php @@ -0,0 +1,107 @@ + + + + 录像页面 + + + + + +
+提示:最好用火狐测试,谷歌浏览器升级了安全策略,谷歌浏览器只能在https下才能利用html5打开摄像头。 + + + + \ No newline at end of file diff --git a/resources/views/vedio/show.php b/resources/views/vedio/show.php new file mode 100755 index 0000000..3030413 --- /dev/null +++ b/resources/views/vedio/show.php @@ -0,0 +1,79 @@ + + + + 录像显示页面 + + + + + +

如果显示空白,说明当前没有人在直播,点击这里直播 + + + \ No newline at end of file diff --git a/runtime/logs/.gitkeep b/runtime/logs/.gitkeep old mode 100644 new mode 100755 diff --git a/runtime/sessions/.gitkeep b/runtime/sessions/.gitkeep old mode 100644 new mode 100755 diff --git a/runtime/uploadfiles/.gitkeep b/runtime/uploadfiles/.gitkeep old mode 100644 new mode 100755