Worker.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | framework
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://framework.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | github开源项目:https://github.com/zoujingli/framework
  12. // +----------------------------------------------------------------------
  13. namespace app\act\socket;
  14. use think\worker\Server;
  15. class Worker extends Server
  16. {
  17. protected $clients = array(); //保存客户端信息
  18. protected $socket = 'websocket://0.0.0.0:2346';
  19. /**
  20. * 收到信息 客户端必须传形如 我自己的uid加密串signature=xxx&to=xxx对方的非加密uid&msg=你好,之后再进行非对称加密
  21. * @param $connection
  22. * @param $data
  23. */
  24. public function onMessage($connection, $data)
  25. {
  26. # 解密data 后续开发
  27. # 解析data
  28. parse_str($data, $data);
  29. # 验证有效性
  30. if (!isset($data['signature']) && !isset($data['to']) && !isset($data['msg'])) {
  31. # 通知客户端有问题
  32. $connection->send('status=2&msg=error');
  33. } else {
  34. $data['user'] = $this->load('api/lib/user')->decode($data['signature']);
  35. if (!$data['user']) {
  36. $connection->send('status=2&msg=error');
  37. }
  38. if (isset($data['user']['uid']) && $data['user']['uid'] > 0 && $data['user']['uid'] != $data['to']) {
  39. # 连接成功
  40. # 检查当前客户端里有没有,没有则保存
  41. if (!isset($this->clients[$data['user']['uid']])) {
  42. # 双方用户信息都由客户端获取
  43. $ip = $connection->getRemoteIp();
  44. $port = $connection->getRemotePort();
  45. $this->clients[$data['user']['uid']]] = array
  46. (
  47. 'ip' => $ip,
  48. 'port' => $port,
  49. 'connection' => $connection
  50. );
  51. }
  52. # 写入数据库,状态为未读
  53. # 检查要发送的信息,有没有在客户端列表里
  54. if (isset($this->clients[$data['to']])) {
  55. # 在线,把消息发过去,客户端看过之后再设置为已读
  56. $this->clients[$data['to']]['connection']->send($data['msg']);
  57. } else {
  58. # 不在线,等该用户上线再读取就行了。不用这里做什么了。
  59. }
  60. } else {
  61. $connection->send('status=2&msg=error');
  62. }
  63. }
  64. }
  65. /**
  66. * 当连接建立时触发的回调函数
  67. * @param $connection
  68. */
  69. public function onConnect($connection)
  70. {
  71. }
  72. /**
  73. * 当连接断开时触发的回调函数
  74. * @param $connection
  75. */
  76. public function onClose($connection)
  77. {
  78. unset($this->clients[$connection->getRemoteIp()]);
  79. }
  80. /**
  81. * 当客户端的连接上发生错误时触发
  82. * @param $connection
  83. * @param $code
  84. * @param $msg
  85. */
  86. public function onError($connection, $code, $msg)
  87. {
  88. echo "error $code $msg\n";
  89. }
  90. /**
  91. * 每个进程启动
  92. * @param $worker
  93. */
  94. public function onWorkerStart($worker)
  95. {
  96. }
  97. }