Socket.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\ChatMessageService;
  4. use App\Services\Common\MemberService;
  5. use App\Services\RedisService;
  6. use Illuminate\Console\Command;
  7. class Socket extends Command
  8. {
  9. public $ws;
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'swoole:socket {op?}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'websocket ';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. $op = $this->argument('op');
  39. $op = $op? $op : 'start';
  40. if($op == 'start'){
  41. echo 'socket start ...';
  42. $this->start();
  43. }else if ($op == 'stop'){
  44. echo 'socket stop ...';
  45. $this->stop();
  46. }
  47. }
  48. /**
  49. * 运行
  50. */
  51. public function start()
  52. {
  53. //创建websocket服务器对象,监听0.0.0.0:7104端口
  54. $this->ws = new \swoole_websocket_server("0.0.0.0", env('SOCKET_PORT','6420'));
  55. // $this->ws->set();
  56. //监听WebSocket连接打开事件
  57. $this->ws->on('open',[$this,'open']);
  58. //监听WebSocket消息事件
  59. $this->ws->on('message',[$this,'message']);
  60. //监听WebSocket主动推送消息事件
  61. $this->ws->on('request',[$this,'request']);
  62. //监听WebSocket连接关闭事件
  63. $this->ws->on('close',[$this,'close']);
  64. $this->ws->start();
  65. }
  66. /**
  67. * 建立连接
  68. * @param $ws
  69. * @param $request
  70. */
  71. public function open($ws, $request){
  72. echo "连接成功:".$request->fd."\n";
  73. $this->ws->push($request->fd,'连接成功');
  74. }
  75. /**
  76. * 接收消息
  77. * @param $ws
  78. * @param $frame
  79. */
  80. public function message($ws,$frame){
  81. if($frame->data == 'ping'){
  82. $this->ws->push($frame->fd, 'pong');
  83. return false;
  84. }
  85. // 消息处理
  86. $data = $frame->data? json_decode($frame->data, true) : [];
  87. $op = isset($data['op'])? $data['op'] : '';
  88. if($data){
  89. switch($op){
  90. case 'bind': // 绑定用户
  91. if(!ChatMessageService::make()->bind($frame->fd, $data)){
  92. $this->ws->push($frame->fd, lang(ChatMessageService::make()->getError()));
  93. return false;
  94. }
  95. break;
  96. default: // 默认聊天
  97. $toUid = isset($data['to_uid'])? intval($data['to_uid']) : 0;
  98. $fromUid = isset($data['from_uid'])? intval($data['from_uid']) : 0;
  99. $messageType = isset($data['message_type'])? $data['message_type'] : 1;
  100. if($toUid<=0 || $fromUid<=0){
  101. return false;
  102. }
  103. if(!ChatMessageService::make()->saveData($data)){
  104. $this->ws->push($frame->fd, lang(ChatMessageService::make()->getError()));
  105. return false;
  106. }
  107. $toBindData = RedisService::get("chats:bind:{$toUid}");
  108. $toFd = isset($toBindData['fd'])? $toBindData['fd'] : 0;
  109. if($toBindData && $toFd){
  110. $toInfo = MemberService::make()->getInfo($toUid);
  111. $fromInfo = MemberService::make()->getInfo($fromUid);
  112. $message = isset($data['message'])? $data['message'] : '';
  113. $data = [
  114. 'type'=> isset($data['type'])? $data['type'] : 1,
  115. 'message_type'=> $messageType,
  116. 'from_uid'=> $fromUid,
  117. 'from_userName'=> isset($fromInfo['username'])? $fromInfo['username'] : '',
  118. 'to_uid'=> isset($data['to_uid'])? $data['to_uid'] : 0,
  119. 'to_userName'=> isset($toInfo['username'])? $toInfo['username'] : '',
  120. 'chat_key'=> getChatKey($fromUid, $toUid),
  121. 'message'=> $messageType==2&&$message? get_image_url($message) : $message,
  122. 'create_time'=> time(),
  123. 'time_text'=> date('m-d H:i'),
  124. 'is_read'=> 1,
  125. 'status'=> 1,
  126. ];
  127. $this->ws->push($toFd, json_encode($data, 256));
  128. }
  129. break;
  130. }
  131. }else{
  132. $this->ws->push($frame->fd, 'not data');
  133. }
  134. }
  135. /**
  136. * 接收请求
  137. * @param $request
  138. * @param $response
  139. */
  140. public function request($request,$response){
  141. }
  142. /**
  143. * 关闭连接
  144. * @param $ws
  145. * @param $fd
  146. */
  147. public function close($ws,$fd=''){
  148. // var_dump($ws);
  149. echo '连接关闭:'.$fd."\n";
  150. $this->ws->close($fd);
  151. }
  152. /**
  153. * 停止运行
  154. */
  155. public function stop($ws)
  156. {
  157. $this->ws->close();
  158. }
  159. }