Event.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author walkor <workerman.net>
  6. *
  7. */
  8. use \Lib\Context;
  9. use \Lib\Gateway;
  10. use \Lib\StatisticClient;
  11. use \Lib\Store;
  12. use \Protocols\GatewayProtocol;
  13. use \Protocols\TextProtocol;
  14. class Event
  15. {
  16. /**
  17. * 当网关有客户端链接上来时触发,一般这里留空
  18. */
  19. public static function onGatewayConnect()
  20. {
  21. Gateway::sendToCurrentUid(TextProtocol::encode("type in your name:"));
  22. }
  23. /**
  24. * 网关有消息时,判断消息是否完整
  25. */
  26. public static function onGatewayMessage($buffer)
  27. {
  28. return TextProtocol::check($buffer);
  29. }
  30. /**
  31. * 此链接的用户没调用GateWay::notifyConnectionSuccess($uid);前(即没有得到验证),都触发onConnect
  32. * 已经调用GateWay::notifyConnectionSuccess($uid);的用户有消息时,则触发onMessage
  33. * @param string $message 一般是传递的账号密码等信息
  34. * @return void
  35. */
  36. public static function onConnect($message)
  37. {
  38. /*
  39. * 通过message验证用户,并获得uid。
  40. * 一般流程这里$message应该包含用户名 密码,然后根据用户名密码从数据库中获取uid
  41. * 这里只是根据时间戳生成uid,高并发下会有小概率uid冲突
  42. */
  43. $uid = self::checkUser($message);
  44. // 不合法踢掉
  45. if(!$uid)
  46. {
  47. // 踢掉
  48. return GateWay::kickCurrentUser(TextProtocol::encode('uid非法'));
  49. }
  50. $_SESSION['name'] = TextProtocol::decode($message);
  51. // [这步是必须的]合法,记录uid到gateway通信地址的映射
  52. GateWay::storeUid($uid);
  53. // [这步是必须的]发送数据包到address对应的gateway,确认connection成功
  54. GateWay::notifyConnectionSuccess($uid);
  55. Gateway::sendToCurrentUid("
  56. chart room login success, your uid is $uid, name is {$_SESSION['name']}
  57. use uid:words send message to one user
  58. use words send message to all\n");
  59. // 广播所有用户,xxx come
  60. GateWay::sendToAll(TextProtocol::encode("{$_SESSION['name']}[$uid] come"));
  61. }
  62. /**
  63. * 当用户断开连接时触发的方法
  64. * @param string $address 和该用户gateway通信的地址
  65. * @param integer $uid 断开连接的用户id
  66. * @return void
  67. */
  68. public static function onClose($uid)
  69. {
  70. // 删除这个用户的gateway通信地址
  71. GateWay::deleteUidAddress($uid);
  72. // 广播 xxx 退出了
  73. GateWay::sendToAll(TextProtocol::encode("{$_SESSION['name']}[$uid] logout"));
  74. }
  75. /**
  76. * 有消息时触发该方法
  77. * @param int $uid 发消息的uid
  78. * @param string $message 消息
  79. * @return void
  80. */
  81. public static function onMessage($uid, $message)
  82. {
  83. $message_data = TextProtocol::decode($message);
  84. // 判断是否是私聊,私聊数据格式 uid:xxxxx
  85. $explode_array = explode(':', $message, 2);
  86. if(count($explode_array) > 1)
  87. {
  88. $to_uid = (int)$explode_array[0];
  89. GateWay::sendToUid($uid, TextProtocol::encode($_SESSION['name'] . "[$uid] said said to [$to_uid] :" . $explode_array[1]));
  90. return GateWay::sendToUid($to_uid, TextProtocol::encode($_SESSION['name'] . "[$uid] said to You :" . $explode_array[1]));
  91. }
  92. // 群聊
  93. return GateWay::sendToAll(TextProtocol::encode($_SESSION['name'] . "[$uid] said :" . $message));
  94. }
  95. /**
  96. * 用户第一次链接时,根据用户传递的消息(一般是用户名 密码)返回当前uid
  97. * 这里只是返回了时间戳相关的一个数字,高并发会有一定的几率uid冲突
  98. * @param string $message
  99. * @return number
  100. */
  101. protected static function checkUser($message)
  102. {
  103. return substr(strval(microtime(true)), 3, 10)*100;
  104. }
  105. }