文件

121 行
4.2 KiB
PHP
可执行文件

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<?php
/**
* 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
* 然后观察一段时间workerman.log看是否有process_timeout异常
*/
//declare(ticks=1);
use \GatewayWorker\Lib\Gateway;
/**
* 主逻辑
* 主要是处理 onConnect onMessage onClose 三个方法
* onConnect 和 onClose 如果不需要可以不用实现并删除
*/
class Events
{
/**
* 当客户端连接时触发
* 如果业务不需此回调可以删除onConnect
*
* @param int $client_id 连接id
*/
/*public static function onConnect($client_id)
{
// 向当前client_id发送数据
Gateway::sendToClient($client_id, "Hello $client_id\r\n");
// 向所有人发送
Gateway::sendToAll("$client_id 在座");
}*/
/**
* 当客户端发来消息时触发
* @param int $client_id 连接id
* @param mixed $message 具体消息
*/
public static function onMessage($client_id, $message)
{
// 向所有人发送
$message_data = json_decode($message, true);
if (!$message_data) {
return;
}
/*Gateway::sendToAll("$client_id 爱你");*/
//判断类型传输对应的值
switch ($message_data['type']) {
//心跳
case 'pong':
return;
case 'login':
//判断是否存在类型 0是支付时间 1是排队事件 2是app推送小程序出票信息
if (!isset($message_data['type_id'])) {
throw new \Exception("\$message_data['type_id'] not set. client_ip:{$_SERVER['REMOTE_ADDR']} \$message:$message");
}
switch ($message_data['type_id']) {
case 0:
//执行支付成功的推送
file_put_contents("./ceshi.txt", json_encode($message_data));
$uid = $message_data['uid'];
Gateway::bindUid($client_id, $uid);
break;
case 1:
//执行用户的核销完成事件
$store_id = $message_data['store_id'];
//这里绑定uid 处理之后的业务流程
Gateway::joinGroup($client_id, $store_id);
break;
}
break;
case 'say':
//发言 以及及时推送消息
switch ($message_data['type_id']) {
case 0:
//执行支付成功的推送
$uid = $message_data['uid'];
$message = $message_data['content'];
$new_message['type'] = "retail1";
$new_message['content'] = $message;
$new_message['type_id'] = 0;
return Gateway::sendToUid($uid, json_encode($new_message));
default:
$uid = $message_data['uid'];
$message = $message_data['content'];
$new_message['type'] = "say";
$new_message['content'] = $message;
$new_message['type_id'] = $message_data['type_id'];
return Gateway::sendToUid($uid, json_encode($new_message));
break;
}
break;
}
}
/**
* 当用户断开连接时触发
* @param int $client_id 连接id
*/
public static function onClose($client_id)
{
// 向所有人发送
// GateWay::sendToAll("$client_id logout\r\n");
}
}