简体   繁体   English

如何将消息发送到通过套接字连接的特定客户端

[英]How can I send a message to a specific client connected through socket

Hello This is my first time working on sockets. 您好,这是我第一次使用套接字。 I have multiple clients which connect to my socket server through a specific port. 我有多个客户端通过特定端口连接到我的套接字服务器。 I want to send a specific message to a specific client. 我想向特定客户端发送特定消息。 how can I do that?. 我怎样才能做到这一点?。

I am using this library 我正在使用这个图书馆

https://github.com/navarr/Sockets https://github.com/navarr/Sockets

This is the code 这是代码

<?php

use Navarr\Socket\Socket;
use Navarr\Socket\Server;

class EchoServer extends Server
{
    const DEFAULT_PORT = 7;

    public function __construct($ip = null, $port = self::DEFAULT_PORT)
    {
        parent::__construct($ip, $port);
        $this->addHook(Server::HOOK_CONNECT, array($this, 'onConnect'));
        $this->addHook(Server::HOOK_INPUT, array($this, 'onInput'));
        $this->addHook(Server::HOOK_DISCONNECT, array($this, 'onDisconnect'));
        $this->run();
    }

    public function onConnect(Server $server, Socket $client, $message)
    {
        echo 'Connection Established',"\n";
    }

    public function onInput(Server $server, Socket $client, $message)
    {
        echo 'Received "',$message,'"',"\n";
        $client->write($message, strlen($message));
    }

    public function onDisconnect(Server $server, Socket $client, $message)
    {
        echo 'Disconnection',"\n";
    }
}

$server = new EchoServer('0.0.0.0');

This line $client->write($message, strlen($message)); 这行$client->write($message, strlen($message)); will send a message to a client if only one client is connected. 如果仅连接一个客户端,它将向客户端发送一条消息。 but if multiple clients connected then how can I send a message to specific client? 但是,如果连接了多个客户端,该如何向特定客户端发送消息?

Add this code inside onConnect function. 将此代码添加到onConnect函数中。

//declare this as global inside EchoServer class so that you can access this outside onConnect function
$connected_clients["userID"] = $client; //use unique id for key

Then to send message, use the userID to access right client: 然后要发送消息,请使用userID访问权限客户端:

$connected_clients["userID"]->write($message, strlen($message));

To get userID , once the client connect to your server request for the client id, example: use JSON for easy communication, send this JSON message 要获取userID ,一旦客户端连接到您的服务器以获取客户端ID的请求,例如:使用JSON进行轻松通信,请发送此JSON消息

{"messageType":"request", "requestType": "identification"} 

to client. 给客户。 On client side process the message and send this JSON message 在客户端处理消息并发送此JSON消息

{"messageType":"response", 
 "body":{"userID":"123456", "accessToken":"ye5473rgfygf737trfeyg3rt764e"}} 

back to server. 回到服务器。 On the server side validate the access token and retrieve userID from the response. 在服务器端,验证访问令牌并从响应中检索userID userID is unique identification number store in database which is assign to each user during there registration to your chat site. userID是数据库中唯一的标识号存储,在注册到您的聊天站点期间,数据库将分配给每个用户。

To know the client that send message, use this JSON message format 要知道发送消息的客户端,请使用此JSON消息格式

{"messageType":"message", 
 "from":"userID", 
 "body":"message here"}

Modify to suit your preference. 修改以适合您的喜好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM