コード例 #1
0
        public static ClientPool GetInstance(string conn, string port, string origin)
        {
            lock (Locker)
            {
                //Safety, remove disconnected instances
                Repository <string, ClientPool> .Remove(p => !p._websocket.Communication.Connected);

                if (!Repository <string, ClientPool> .ContainsKey(conn + port))
                {
                    var x = new ClientPool
                    {
                        _conn           = conn,
                        _port           = port,
                        _textQueue      = new BlockingCollection <IMessage>(),
                        _jsonSerializer = new XSocketJsonSerializer()
                    };
                    x._websocket = new XSocketClient(x._conn, x._port, origin);
                    ((XSocketClient)x._websocket).OnConnected += (sender, args) => Task.Factory.StartNew(() =>
                    {
                        //Will send messages to the XSockets server as soon as there is messages in the queue.
                        foreach (var v in x._textQueue.GetConsumingEnumerable())
                        {
                            var ctrl =
                                Repository <string, ClientPool> .GetById(x._conn)._websocket.Controller(v.Controller);
                            if (ctrl.ClientInfo.ConnectionId == Guid.Empty)
                            {
                                ctrl.ClientInfo.ConnectionId = Guid.NewGuid();
                            }
                            ctrl.Publish(v);
                        }
                    });

                    x._websocket.OnDisconnected += (sender, args) => Repository <string, ClientPool> .Remove(x._conn + port);

                    x._websocket.Open();
                    Repository <string, ClientPool> .AddOrUpdate(conn + port, x);
                }
            }
            return(Repository <string, ClientPool> .GetById(conn + port));
        }
コード例 #2
0
 public static ClientPool GetInstance(string conn, string port, string origin)
 {
     lock (Locker)
     {
         //Safety, remove disconnected instances
         Repository<string, ClientPool>.Remove(p => !p._websocket.Communication.Connected);
         if (!Repository<string, ClientPool>.ContainsKey(conn+port))
         {
             var x = new ClientPool
             {
                 _conn = conn,
                 _port = port,
                 _textQueue = new BlockingCollection<IMessage>(),
                 _jsonSerializer = new XSocketJsonSerializer()
             };
             x._websocket = new XSocketClient(x._conn,  x._port, origin);
             ((XSocketClient)x._websocket).OnConnected += (sender, args) => Task.Factory.StartNew(() =>
             {
                 //Will send messages to the XSockets server as soon as there is messages in the queue.
                 foreach (var v in x._textQueue.GetConsumingEnumerable())
                 {
                     var ctrl =
                         Repository<string, ClientPool>.GetById(x._conn)._websocket.Controller(v.Controller);
                     if (ctrl.ClientInfo.ConnectionId == Guid.Empty)
                     {
                         ctrl.ClientInfo.ConnectionId = Guid.NewGuid();
                     }
                     ctrl.Publish(v);
                 }
             });
             
             x._websocket.OnDisconnected += (sender, args) => Repository<string, ClientPool>.Remove(x._conn+port);
             x._websocket.Open();
             Repository<string, ClientPool>.AddOrUpdate(conn + port, x);
         }
     }
     return Repository<string, ClientPool>.GetById(conn+port);
 }