最快的方式写入多个套接字连接 [英] Fastest way to write to multiple socket connections

查看:89
本文介绍了最快的方式写入多个套接字连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了下面的类,以接受来自客户端应用程序的传入连接 - 使用我想在同一时间写同样UTFBytes到每个客户端发送的功能 - 这可能吗?如果还是不行,什么是按顺序写入它们最快的方式。

 公共类ProjectorClients
{
    私人VAR _serverSocket:ServerSocket的;
    私人VAR _clients:矢量<插座>。 =新矢量<插座取代;

    私有函数ProjectorClients():无效
    {
        _serverSocket =新的ServerSocket();
        _serverSocket.addEventListener(ServerSocketConnectEvent.CONNECT,为onConnect)
        _serverSocket.bind(888);
        _serverSocket.listen();
    }

    私有函数的onConnect(E:ServerSocketConnectEvent中):无效
    {
        跟踪(客户端连接);
        e.socket.addEventListener(ProgressEvent.SOCKET_DATA,昂达);
        e.socket.addEventListener(Event.CLOSE,onConnectionClosed);
        _clients.push(e.socket);
        跟踪(连接的客户端的数量:+ _clients.length);
    }

    公共职能发送(命令:字符串):无效
    {
        每个(VAR ClientSocket的:插座_clients)
        {
            如果(clientSocket.connected)
            {
                clientSocket.writeUTFBytes(命令);
                clientSocket.flush();
            }
        }
    }

    私有函数昂达(E:ProgressEvent):无效
    {
        跟踪(数据接收);
    }

    私有函数onConnectionClosed(五:事件):无效
    {
        跟踪(客户端关闭套接字);
        对于(VAR我:= 0; I< _clients.length;我++)
        {
            如果(_clients [我] == e.currentTarget)
            {
                _clients.splice(ⅰ,1);
                打破;
            }
        }
        跟踪(连接的客户端的数量:+ _clients.length);
    }

}
 

解决方案

正如刚才@eSniff,你需要一个发布订阅此模块。因为它需要裸露最少的步骤来设置Redis的将是一个更好的选择。传入的​​连接将订阅队列,你可以发布的数据,让所有的客户端收到同一时间。请参考下面的链接,更好的理解。

http://redis.io/topics/pubsub

I am using the following class to accept incoming connections from client applications - using the send function I want to write the same UTFBytes to each client at the same time - is this possible? or if not, what would be the fastest way to write to them sequentially.

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);
    }

}

解决方案

As mentioned by @eSniff, you need a publish subscribe module here. Redis would be a better option as it requires bare minimal steps to set up. The incoming connections will subscribe to the queue and you can publish the data, so that all the client receive it the same time. Please refer to the link below for better understanding.

http://redis.io/topics/pubsub

这篇关于最快的方式写入多个套接字连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆