Events.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. use \think\Db;
  27. class Events
  28. {
  29. public static function onWorkerStart(){
  30. /*global $db;
  31. $my_config = array(
  32. 'host' => '127.0.0.1',
  33. 'port' => '3306',
  34. 'user' => 'youeryuan',
  35. 'password' => 'XwXXAFbp8kaYsLKF',
  36. 'dbname' => 'youeryuan',
  37. 'charset' => 'utf8',
  38. );
  39. $db = new DbConnection($my_config);*/
  40. echo 'onWorkerStart';
  41. }
  42. /**
  43. * 有消息时
  44. * @param int $client_id
  45. * @param mixed $message
  46. */
  47. public static function onMessage($client_id, $message)
  48. {
  49. dump($message);
  50. file_put_contents('1.json',$message.PHP_EOL,FILE_APPEND);
  51. return ;
  52. }
  53. /**
  54. * 当客户端断开连接时
  55. * @param integer $client_id 客户端id
  56. */
  57. public static function onClose($client_id)
  58. {
  59. // debug
  60. /* echo "client:{$_SERVER['REMOTE_ADDR']}:{$_SERVER['REMOTE_PORT']} gateway:{$_SERVER['GATEWAY_ADDR']}:{$_SERVER['GATEWAY_PORT']} client_id:$client_id onClose:''\n";
  61. // 从房间的客户端列表中删除
  62. if(isset($_SESSION['room_id']))
  63. {
  64. $room_id = $_SESSION['room_id'];
  65. $new_message = array('type'=>'logout', 'from_client_id'=>$client_id, 'from_client_name'=>$_SESSION['client_name'], 'time'=>date('Y-m-d H:i:s'));
  66. Gateway::sendToGroup($room_id, json_encode($new_message));
  67. }*/
  68. }
  69. }