我正在使用以下类接受来自客户端应用程序的传入连接 - 使用send函数我想同时向每个客户端写入相同的UTFBytes - 这可能吗?或者如果没有,那么按顺序写入它们的最快方法是什么。
public class ProjectorClients
{
private var _serverSocket:ServerSocket;
private var _clients:Vector.<Socket> = new Vector.<Socket>;
private function ProjectorClients():void
{
_serverSocket = new ServerSocket();
_serverSocket.addEventListener(ServerSocketConnectEvent.CONNECT, onConnect)
_serverSocket.bind(888);
_serverSocket.listen();
}
private function onConnect(e:ServerSocketConnectEvent):void
{
trace("Client is connected");
e.socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);
e.socket.addEventListener(Event.CLOSE, onConnectionClosed);
_clients.push(e.socket);
trace("Number of connected clients: " + _clients.length);
}
public function send(command:String):void
{
for each(var clientSocket:Socket in _clients)
{
if (clientSocket.connected)
{
clientSocket.writeUTFBytes(command);
clientSocket.flush();
}
}
}
private function onData(e:ProgressEvent):void
{
trace("data received");
}
private function onConnectionClosed(e:Event):void
{
trace("Client Socket is Closed");
for (var i:int = 0; i < _clients.length; i++)
{
if (_clients[i] == e.currentTarget)
{
_clients.splice(i,1);
break;
}
}
trace("Number of connected clients: " + _clients.length);
}
}
答案 0 :(得分:0)
如@eSniff所述,您需要一个发布订阅模块。 Redis是一个更好的选择,因为它只需要很少的设置步骤。传入连接将订阅队列,您可以发布数据,以便所有客户端同时接收它。请参阅以下链接以便更好地理解。