quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
 * 用于名片聊天的业务逻辑
 * 处理 onConnect onMessage onClose 三个方法
 */
 
namespace app\gateway;
 
use \GatewayWorker\Lib\Gateway;
use app\api\model\plus\business\chat\Chat as BusinessChatModel;
use app\api\model\plus\business\chat\Participant as BusinessParticipantModel;
use Workerman\Lib\Timer;
use Workerman\Worker;
use think\worker\Application;
 
/**
 * 名片聊天主逻辑
 */
class BusinessEvents
{
    /**
     * onWorkerStart 事件回调
     * 当businessWorker进程启动时触发。每个进程生命周期内都只会触发一次
     *
     * @access public
     * @param \Workerman\Worker $businessWorker
     * @return void
     */
    public static function onWorkerStart(Worker $businessWorker)
    {
        $app = new Application;
        $app->initialize();
    }
 
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     *
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        // 向当前client_id发送数据
        $data['client_id'] = $client_id;
        $data['type'] = 'init';//心跳
        Gateway::sendToClient($client_id, json_encode($data));
    }
 
    /**
     * 当客户端发来消息时触发
     * @param int $client_id 连接id
     * @param mixed $message 具体消息
     */
    public static function onMessage($client_id, $message)
    {
        $data = json_decode($message, 1);
        $to = !empty($data['to_user_id']) ? 'user_' . $data['to_user_id'] : 0;
        $from_id = 'user_' . $data['user_id'];
        if ($data['type'] == 'bind') {
            // 绑定client_id和user_id
            Gateway::bindUid($client_id, $from_id);
 
        } else if ($data['type'] == 'ping') {
            //心跳
            $data['Online'] = $to && Gateway::isUidOnline($to) ? 'off' : 'on';
            Gateway::sendToUid($from_id, json_encode($data));
        } else if ($data['type'] == 'close') {
            //断开链接
            Gateway::unbindUid($client_id, $from_id);
        } else if ($data['type'] == 'read') {
            // 消息已读处理
            $chatModel = new BusinessChatModel();
            $chatModel->markAsRead($data['chat_id'], $data['user_id']);
 
            // 更新参与者最后阅读时间
            $participantModel = new BusinessParticipantModel();
            $participantModel->updateLastReadTime($data['conversation_id'], $data['user_id']);
            if ($to && Gateway::isUidOnline($to)) {
                Gateway::sendToUid($to, json_encode($data));
            }
        } else if ($data['type'] !== 'ping' && $data['type'] !== 'close') {//正常发送消息
            // 绑定client_id和user_id
            Gateway::bindUid($client_id, $from_id);
            // 保存消息到数据库
            $Chat = new BusinessChatModel;
            $Chat->sendMessage($data['conversation_id'], $data['user_id'], $data['content'], 0, $data['app_id']);
            // 检查接收方是否在线
            if ($to && Gateway::isUidOnline($to)) {
                $data['send_time'] = time();
                $data['chat_id']=$Chat->chat_id;
                Gateway::sendToUid($to, json_encode($data));
            }
        }
    }
 
    /**
     * 当用户断开连接时触发
     * @param int $client_id 连接id
     */
    public static function onClose($client_id)
    {
        // 向所有人发送
        //GateWay::sendToAll("$client_id logout\r\n");
    }
}