【问题标题】:[Websocket][Chat] - How do I find out if a message was send "from client" or "from other user"[Websocket][Chat] - 如何确定消息是“从客户端”还是“从其他用户”发送的
【发布时间】:2022-01-05 13:11:15
【问题描述】:

如果在用户聊天应用中消息是“从客户端”或“从任何其他用户”发送的,则以下方法 (check video at this Link) 以 php 形式描述。

       public function onMessage(ConnectionInterface $from, $msg) {
        $numRecv = count($this->clients) - 1;
        echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
            , $from->resourceId, $msg, $numRecv, o  == 1 ? '' : 's');
    
        $data=json_decode($msg,true);

        foreach($this->$clients as $client){
        if ($from == $client){
            $data['from']='Me';
        }else{
            $data['from']=get_current_user_id();    
        }
        
        $client->send(json_encode($data));
        
    }

我想检测上述过程而不在 php 中调用这个函数。这是否可以通过 websockets 代码中的 javascript 来实现,例如在 package.json 文件中?

我想检测消息是从套接字发送还是从套接字接收,因为我必须根据发送消息的人(客户端用户或其他人)向左或向右排列它们。

提前感谢您的支持

J.

【问题讨论】:

    标签: websocket chat ratchet


    【解决方案1】:

    在 conn.onmessage 函数的主体中,您可以确定它是从服务器接收到的消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-26
      • 2022-07-15
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多