【发布时间】:2018-05-14 23:02:12
【问题描述】:
你好这是我第一次在套接字上工作。我有多个客户端通过特定端口连接到我的套接字服务器。我想向特定客户发送特定消息。我怎样才能做到这一点?。
我正在使用这个library
https://github.com/navarr/Sockets
这是代码
<?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');
如果只有一个客户端连接,这行$client->write($message, strlen($message)); 将向客户端发送一条消息。但是如果连接了多个客户端,那么如何向特定客户端发送消息?
【问题讨论】:
-
您需要跟踪
session中的用户吗?然后向他们发送相应的反馈