123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?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);
- /**
- * 聊天主逻辑
- * 主要是处理 onMessage onClose
- */
- use \GatewayWorker\Lib\Gateway;
- use \GatewayWorker\Lib\DbConnection;
- class Events
- {
- public static function onWorkerStart(){
- /*global $db;
- $my_config = array(
- 'host' => '127.0.0.1',
- 'port' => '3306',
- 'user' => 'youeryuan_online',
- 'password' => 'XwXXAFbp8kaYsLKF_1',
- 'dbname' => 'youeryuan_online',
- 'charset' => 'utf8',
- );
- $db = new DbConnection($my_config);*/
- }
-
- /**
- * 有消息时
- * @param int $client_id
- * @param mixed $message
- */
- public static function onMessage($client_id, $message)
- {
- $hex = [];
- $receiver = '';
- $nfcid = '';
- for($i=0; $i<strlen($message); $i++)
- {
- //解析ASCII,16进制,小写
- $one_message = strtolower(dechex(ord($message[$i])));
- //数组
- $hex[] = $one_message;
- //设备id
- if($i == 1){ $receiver = $one_message;}
- //nfcid
- if(in_array($i,[5,6,7,8])){
- $nfcid .= $one_message;
- }
- }
- if(count($hex) != 11){
- return;
- }
- $hexjson = json_encode($hex);
- echo $hexjson.PHP_EOL;
- /*file_put_contents('1.json',$hexjson.PHP_EOL,FILE_APPEND);*/
- //入库
- return ;
- }
-
- /**
- * 当客户端断开连接时
- * @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));
- }*/
- }
-
- }
|