|
|
@@ -43,12 +43,12 @@ class Socket extends Command
|
|
|
public function handle()
|
|
|
{
|
|
|
$op = $this->argument('op');
|
|
|
- $op = $op? $op : 'start';
|
|
|
- if($op == 'start'){
|
|
|
- echo 'socket start ...';
|
|
|
+ $op = $op ? $op : 'start';
|
|
|
+ if ($op == 'start') {
|
|
|
+ echo "socket start ...\n";
|
|
|
$this->start();
|
|
|
- }else if ($op == 'stop'){
|
|
|
- echo 'socket stop ...';
|
|
|
+ } else if ($op == 'stop') {
|
|
|
+ echo "socket stop ...\n";
|
|
|
$this->stop();
|
|
|
}
|
|
|
}
|
|
|
@@ -59,21 +59,21 @@ class Socket extends Command
|
|
|
public function start()
|
|
|
{
|
|
|
//创建websocket服务器对象,监听0.0.0.0:7104端口
|
|
|
- $this->ws = new \swoole_websocket_server("0.0.0.0", env('SOCKET_PORT','6420'));
|
|
|
+ $this->ws = new \swoole_websocket_server("0.0.0.0", env('SOCKET_PORT', '6420'));
|
|
|
|
|
|
// $this->ws->set();
|
|
|
|
|
|
//监听WebSocket连接打开事件
|
|
|
- $this->ws->on('open',[$this,'open']);
|
|
|
+ $this->ws->on('open', [$this, 'open']);
|
|
|
|
|
|
//监听WebSocket消息事件
|
|
|
- $this->ws->on('message',[$this,'message']);
|
|
|
+ $this->ws->on('message', [$this, 'message']);
|
|
|
|
|
|
//监听WebSocket主动推送消息事件
|
|
|
- $this->ws->on('request',[$this,'request']);
|
|
|
+ $this->ws->on('request', [$this, 'request']);
|
|
|
|
|
|
//监听WebSocket连接关闭事件
|
|
|
- $this->ws->on('close',[$this,'close']);
|
|
|
+ $this->ws->on('close', [$this, 'close']);
|
|
|
|
|
|
$this->ws->start();
|
|
|
}
|
|
|
@@ -83,9 +83,10 @@ class Socket extends Command
|
|
|
* @param $ws
|
|
|
* @param $request
|
|
|
*/
|
|
|
- public function open($ws, $request){
|
|
|
- echo "连接成功:".$request->fd."\n";
|
|
|
- $this->ws->push($request->fd,'连接成功');
|
|
|
+ public function open($ws, $request)
|
|
|
+ {
|
|
|
+ echo "连接成功:" . $request->fd . "\n";
|
|
|
+ $this->ws->push($request->fd, json_encode(['message'=>'连接成功'], 256));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -93,63 +94,70 @@ class Socket extends Command
|
|
|
* @param $ws
|
|
|
* @param $frame
|
|
|
*/
|
|
|
- public function message($ws,$frame){
|
|
|
- if($frame->data == 'ping'){
|
|
|
+ public function message($ws, $frame)
|
|
|
+ {
|
|
|
+ if ($frame->data == 'ping') {
|
|
|
$this->ws->push($frame->fd, 'pong');
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// 消息处理
|
|
|
- $data = $frame->data? json_decode($frame->data, true) : [];
|
|
|
- $op = isset($data['op'])? $data['op'] : '';
|
|
|
- if($data){
|
|
|
- switch($op){
|
|
|
- case 'bind': // 绑定用户
|
|
|
- if(!ChatMessageService::make()->bind($frame->fd, $data)){
|
|
|
- $this->ws->push($frame->fd, lang(ChatMessageService::make()->getError()));
|
|
|
- return false;
|
|
|
- }
|
|
|
- break;
|
|
|
- default: // 默认聊天
|
|
|
- $toUid = isset($data['to_uid'])? intval($data['to_uid']) : 0;
|
|
|
- $fromUid = isset($data['from_uid'])? intval($data['from_uid']) : 0;
|
|
|
- $messageType = isset($data['message_type'])? $data['message_type'] : 1;
|
|
|
- if($toUid<=0 || $fromUid<=0){
|
|
|
+ $data = $frame->data ? json_decode($frame->data, true) : [];
|
|
|
+ $op = isset($data['op']) ? $data['op'] : '';
|
|
|
+ $userId = isset($data['from_uid'])? intval($data['from_uid']) : 0;
|
|
|
+ $chatKey = isset($data['chat_key'])? trim($data['chat_key']) : '';
|
|
|
+ if($userId && $frame->fd){
|
|
|
+ RedisService::set("chats:bind:{$userId}", ['fd'=> $frame->fd, 'user_id'=> $userId,'chat_key'=>$chatKey], 86400);
|
|
|
+ }
|
|
|
+ if ($data) {
|
|
|
+ switch ($op) {
|
|
|
+ case 'chat': // 默认聊天
|
|
|
+ $toUid = isset($data['to_uid']) ? intval($data['to_uid']) : 0;
|
|
|
+ $fromUid = isset($data['from_uid']) ? intval($data['from_uid']) : 0;
|
|
|
+ $messageType = isset($data['message_type']) ? $data['message_type'] : 1;
|
|
|
+
|
|
|
+ if ($toUid <= 0 || $fromUid <= 0) {
|
|
|
return false;
|
|
|
}
|
|
|
- if(!ChatMessageService::make()->saveData($data)){
|
|
|
- $this->ws->push($frame->fd, lang(ChatMessageService::make()->getError()));
|
|
|
+
|
|
|
+ if (!ChatMessageService::make()->saveData($data)) {
|
|
|
+ $data = ['message'=> lang(ChatMessageService::make()->getError())];
|
|
|
+ $this->ws->push($frame->fd, json_encode($data, 256));
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ $toInfo = MemberService::make()->getInfo($toUid);
|
|
|
+ $fromInfo = MemberService::make()->getInfo($fromUid);
|
|
|
+ $message = isset($data['message']) ? $data['message'] : '';
|
|
|
+ $chatKey = getChatKey($fromUid, $toUid);
|
|
|
+ $data = [
|
|
|
+ 'type' => isset($data['type']) ? $data['type'] : 1,
|
|
|
+ 'message_type' => $messageType,
|
|
|
+ 'from_uid' => $fromUid,
|
|
|
+ 'from_userName' => isset($fromInfo['username']) ? format_account($fromInfo['username']) : '客服',
|
|
|
+ 'to_uid' => isset($data['to_uid']) ? $data['to_uid'] : 0,
|
|
|
+ 'to_userName' => isset($toInfo['username']) ? format_account($toInfo['username']) : '客服',
|
|
|
+ 'chat_key' => $chatKey,
|
|
|
+ 'message' => $message,
|
|
|
+ 'message_url' => $messageType == 2 && $message ? get_image_url($message) : '',
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'time_text' => date('m-d H:i'),
|
|
|
+ 'is_read' => 1,
|
|
|
+ 'status' => 1,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $this->ws->push($frame->fd, json_encode($data, 256));
|
|
|
$toBindData = RedisService::get("chats:bind:{$toUid}");
|
|
|
- $toFd = isset($toBindData['fd'])? $toBindData['fd'] : 0;
|
|
|
- if($toBindData && $toFd){
|
|
|
- $toInfo = MemberService::make()->getInfo($toUid);
|
|
|
- $fromInfo = MemberService::make()->getInfo($fromUid);
|
|
|
- $message = isset($data['message'])? $data['message'] : '';
|
|
|
- $data = [
|
|
|
- 'type'=> isset($data['type'])? $data['type'] : 1,
|
|
|
- 'message_type'=> $messageType,
|
|
|
- 'from_uid'=> $fromUid,
|
|
|
- 'from_userName'=> isset($fromInfo['username'])? $fromInfo['username'] : '',
|
|
|
- 'to_uid'=> isset($data['to_uid'])? $data['to_uid'] : 0,
|
|
|
- 'to_userName'=> isset($toInfo['username'])? $toInfo['username'] : '',
|
|
|
- 'chat_key'=> getChatKey($fromUid, $toUid),
|
|
|
- 'message'=> $messageType==2&&$message? get_image_url($message) : $message,
|
|
|
- 'create_time'=> time(),
|
|
|
- 'time_text'=> date('m-d H:i'),
|
|
|
- 'is_read'=> 1,
|
|
|
- 'status'=> 1,
|
|
|
- ];
|
|
|
+ $toFd = isset($toBindData['fd']) ? $toBindData['fd'] : 0;
|
|
|
+ $toChatKey = isset($toBindData['chat_key']) ? $toBindData['chat_key'] : '';
|
|
|
+ if ($toBindData && $toFd && $toChatKey == $chatKey) {
|
|
|
$this->ws->push($toFd, json_encode($data, 256));
|
|
|
}
|
|
|
|
|
|
break;
|
|
|
-
|
|
|
}
|
|
|
- }else{
|
|
|
- $this->ws->push($frame->fd, 'not data');
|
|
|
+ } else {
|
|
|
+ $this->ws->push($frame->fd, '{message:no data}');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -158,7 +166,8 @@ class Socket extends Command
|
|
|
* @param $request
|
|
|
* @param $response
|
|
|
*/
|
|
|
- public function request($request,$response){
|
|
|
+ public function request($request, $response)
|
|
|
+ {
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -167,9 +176,10 @@ class Socket extends Command
|
|
|
* @param $ws
|
|
|
* @param $fd
|
|
|
*/
|
|
|
- public function close($ws,$fd=''){
|
|
|
+ public function close($ws, $fd = '')
|
|
|
+ {
|
|
|
// var_dump($ws);
|
|
|
- echo '连接关闭:'.$fd."\n";
|
|
|
+ echo '连接关闭:' . $fd . "\n";
|
|
|
$this->ws->close($fd);
|
|
|
}
|
|
|
|