up. 添加gateway event类
This commit is contained in:
parent
87dc171497
commit
0247c8d713
@ -1,19 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace app\event;
|
||||
|
||||
/**
|
||||
* This file is part of workerman.
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* For full copyright and license information, please see the MIT-LICENSE.txt
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*
|
||||
* @author walkor<walkor@workerman.net>
|
||||
* @copyright walkor<walkor@workerman.net>
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
/**
|
||||
* 用于检测业务代码死循环或者长时间阻塞等问题
|
||||
* 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
|
||||
@ -25,39 +12,36 @@ namespace app\event;
|
||||
* 聊天主逻辑
|
||||
* 主要是处理 onMessage onClose
|
||||
*/
|
||||
|
||||
use \GatewayWorker\Lib\Gateway;
|
||||
|
||||
class AdminGatewayEvents
|
||||
{
|
||||
|
||||
/**
|
||||
* 有消息时
|
||||
* @param int $client_id
|
||||
* @param mixed $message
|
||||
*/
|
||||
public static function onMessage($client_id, $message)
|
||||
public static function onMessage(int $client_id, string $message):void
|
||||
{
|
||||
// debug
|
||||
echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id session:".json_encode($_SESSION)." onMessage:".$message."\n";
|
||||
|
||||
var_dump($message);
|
||||
// 客户端传递的是json数据
|
||||
$message_data = json_decode($message, true);
|
||||
if(!$message_data)
|
||||
{
|
||||
if (!$message_data) {
|
||||
return;
|
||||
}
|
||||
$message_data = array_merge([
|
||||
'type' => '',
|
||||
'event'=> '',
|
||||
], $message_data);
|
||||
|
||||
|
||||
// 根据类型执行不同的业务
|
||||
switch($message_data['type'])
|
||||
{
|
||||
switch ($message_data['type']) {
|
||||
// 客户端回应服务端的心跳
|
||||
case 'pong':
|
||||
return;
|
||||
case 'system':
|
||||
switch ($message_data['event']) {
|
||||
// 客户端登录 message格式: {type:login, name:xx, room_id:1} ,添加到客户端,广播给所有客户端xx进入聊天室
|
||||
case 'login':
|
||||
// 判断是否有房间号
|
||||
if(!isset($message_data['room_id']))
|
||||
{
|
||||
if (!isset($message_data['room_id'])) {
|
||||
throw new \Exception("\$message_data['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']} \$message:$message");
|
||||
}
|
||||
|
||||
@ -69,8 +53,7 @@ class AdminGatewayEvents
|
||||
|
||||
// 获取房间内所有用户列表
|
||||
$clients_list = Gateway::getClientSessionsByGroup($room_id);
|
||||
foreach($clients_list as $tmp_client_id=>$item)
|
||||
{
|
||||
foreach ($clients_list as $tmp_client_id => $item) {
|
||||
$clients_list[$tmp_client_id] = $item['client_name'];
|
||||
}
|
||||
$clients_list[$client_id] = $client_name;
|
||||
@ -88,16 +71,14 @@ class AdminGatewayEvents
|
||||
// 客户端发言 message: {type:say, to_client_id:xx, content:xx}
|
||||
case 'say':
|
||||
// 非法请求
|
||||
if(!isset($_SESSION['room_id']))
|
||||
{
|
||||
if (!isset($_SESSION['room_id'])) {
|
||||
throw new \Exception("\$_SESSION['room_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']}");
|
||||
}
|
||||
$room_id = $_SESSION['room_id'];
|
||||
$client_name = $_SESSION['client_name'];
|
||||
|
||||
// 私聊
|
||||
if($message_data['to_client_id'] != 'all')
|
||||
{
|
||||
if ($message_data['to_client_id'] != 'all') {
|
||||
$new_message = array(
|
||||
'type' => 'say',
|
||||
'from_client_id' => $client_id,
|
||||
@ -108,7 +89,8 @@ class AdminGatewayEvents
|
||||
);
|
||||
Gateway::sendToClient($message_data['to_client_id'], json_encode($new_message));
|
||||
$new_message['content'] = "<b>你对" . htmlspecialchars($message_data['to_client_name']) . "说: </b>" . nl2br(htmlspecialchars($message_data['content']));
|
||||
return Gateway::sendToCurrentClient(json_encode($new_message));
|
||||
Gateway::sendToCurrentClient(json_encode($new_message));
|
||||
return;
|
||||
}
|
||||
|
||||
$new_message = array(
|
||||
@ -119,26 +101,22 @@ class AdminGatewayEvents
|
||||
'content' => nl2br(htmlspecialchars($message_data['content'])),
|
||||
'time' => date('Y-m-d H:i:s'),
|
||||
);
|
||||
return Gateway::sendToGroup($room_id ,json_encode($new_message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当客户端断开连接时
|
||||
* @param integer $client_id 客户端id
|
||||
*/
|
||||
public static function onClose($client_id)
|
||||
{
|
||||
// debug
|
||||
echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
|
||||
|
||||
// 从房间的客户端列表中删除
|
||||
if(isset($_SESSION['room_id']))
|
||||
{
|
||||
$room_id = $_SESSION['room_id'];
|
||||
$new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
|
||||
Gateway::sendToGroup($room_id, json_encode($new_message));
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
public static function onClose(int $client_id)
|
||||
{
|
||||
var_dump(['client_id' => $client_id, 'r'=>'CLOSET']);
|
||||
// // 从房间的客户端列表中删除
|
||||
// if (isset($_SESSION['room_id'])) {
|
||||
// $room_id = $_SESSION['room_id'];
|
||||
// $new_message = array('type' => 'logout', 'from_client_id' => $client_id, 'from_client_name' => $_SESSION['client_name'], 'time' => date('Y-m-d H:i:s'));
|
||||
// Gateway::sendToGroup($room_id, json_encode($new_message));
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user