Events.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. use \GatewayWorker\Lib\Gateway;
  21. /**
  22. * 主逻辑
  23. * 主要是处理 onConnect onMessage onClose 三个方法
  24. * onConnect 和 onClose 如果不需要可以不用实现并删除
  25. */
  26. class Events
  27. {
  28. /**
  29. * 当客户端连接时触发
  30. * 如果业务不需此回调可以删除onConnect
  31. *
  32. * @param int $client_id 连接id
  33. */
  34. public static function onConnect($client_id)
  35. {
  36. echo "连接成功{$client_id}\n";
  37. $auth_timer_id = \Workerman\Timer::add(20, function($client_id){
  38. // echo "未登录断开连接\n";
  39. Gateway::closeClient($client_id);
  40. }, array($client_id), false);
  41. Gateway::updateSession($client_id, array('auth_timer_id' => $auth_timer_id));
  42. }
  43. /**
  44. * 当客户端发来消息时触发
  45. * @param int $client_id 连接id
  46. * @param mixed $message 具体消息
  47. */
  48. public static function onMessage($client_id, $message)
  49. {
  50. $_SESSION = Gateway::getSession($client_id);
  51. $msg = json_decode($message,true);
  52. if($msg['type'] == 'login'){
  53. \Workerman\Timer::del($_SESSION['auth_timer_id']);
  54. Gateway::bindUid($client_id,$msg['userId']);
  55. Gateway::updateSession($client_id,['userId'=>$msg['userId']]);
  56. Gateway::sendToClient($client_id, self::cjson('login',[],'登录成功'));
  57. echo "登录成功\n";
  58. } else if($msg['type'] == 'ping'){
  59. Gateway::sendToClient($client_id, self::cjson('pong',[],''));
  60. return;
  61. }
  62. if(!$_SESSION['userId']){ // 未登录
  63. Gateway::sendToClient($client_id, self::cjson('unlogin',[],'未登录'));
  64. return;
  65. }
  66. switch($msg['type']){
  67. case 'message':
  68. break;
  69. }
  70. }
  71. /**
  72. * 当用户断开连接时触发
  73. * @param int $client_id 连接id
  74. */
  75. public static function onClose($client_id){
  76. Gateway::sendToClient($client_id, self::cjson('close',[],'断开连接'));
  77. echo "断开连接\n";
  78. }
  79. /**
  80. * 获取响应参数
  81. * @param string $type
  82. * @param array $data
  83. * @param string $msg
  84. * @return string
  85. */
  86. private static function cjson($type='',$data=[],$msg=''){
  87. $jsonarr = [
  88. 'type' => $type,
  89. 'data' => $data,
  90. 'msg' => $msg
  91. ];
  92. return json_encode($jsonarr);
  93. }
  94. }