Events.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. /**
  15. * 用于检测业务代码死循环或者长时间阻塞等问题
  16. * 如果发现业务卡死,可以将下面declare打开(去掉//注释),并执行php start.php reload
  17. * 然后观察一段时间workerman.log看是否有process_timeout异常
  18. */
  19. //declare(ticks=1);
  20. /**
  21. * 聊天主逻辑
  22. * 主要是处理 onMessage onClose
  23. */
  24. use \GatewayWorker\Lib\Gateway;
  25. use \GatewayWorker\Lib\DbConnection;
  26. class Events
  27. {
  28. public static function onWorkerStart(){
  29. /*global $db;
  30. $my_config = array(
  31. 'host' => '127.0.0.1',
  32. 'port' => '3306',
  33. 'user' => 'youeryuan_online',
  34. 'password' => 'XwXXAFbp8kaYsLKF_1',
  35. 'dbname' => 'youeryuan_online',
  36. 'charset' => 'utf8',
  37. );
  38. $db = new DbConnection($my_config);*/
  39. }
  40. /**
  41. * 有消息时
  42. * @param int $client_id
  43. * @param mixed $message
  44. */
  45. public static function onMessage($client_id, $message)
  46. {
  47. $hex = [];
  48. $receiver = '';
  49. $nfcid = '';
  50. for($i=0; $i<strlen($message); $i++)
  51. {
  52. //解析ASCII,16进制,小写
  53. $one_message = strtolower(dechex(ord($message[$i])));
  54. //数组
  55. $hex[] = $one_message;
  56. //设备id
  57. if($i == 1){ $receiver = $one_message;}
  58. //nfcid
  59. if(in_array($i,[5,6,7,8])){
  60. $nfcid .= $one_message;
  61. }
  62. }
  63. if(count($hex) != 11){
  64. return;
  65. }
  66. $hexjson = json_encode($hex);
  67. echo $hexjson.PHP_EOL;
  68. /*file_put_contents('1.json',$hexjson.PHP_EOL,FILE_APPEND);*/
  69. //入库
  70. return ;
  71. }
  72. /**
  73. * 当客户端断开连接时
  74. * @param integer $client_id 客户端id
  75. */
  76. public static function onClose($client_id)
  77. {
  78. // debug
  79. /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
  80. // 从房间的客户端列表中删除
  81. if(isset($_SESSION['room_id']))
  82. {
  83. $room_id = $_SESSION['room_id'];
  84. $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
  85. Gateway::sendToGroup($room_id, json_encode($new_message));
  86. }*/
  87. }
  88. }