示例#1
0
 public static ClientPool GetInstance(string conn, string origin)
 {
     lock (Locker)
     {
         //Safety, remove disconnected instances
         Repository<string, ClientPool>.Remove(p => !p._websocket.Socket.Connected);
         if (!Repository<string, ClientPool>.ContainsKey(conn))
         {
             var x = new ClientPool
             {
                 _conn = conn,
                 _textQueue = new BlockingCollection<IMessage>(),
                 _jsonSerializer = new XSocketJsonSerializer()
             };
             x._websocket = new XSocketClient(x._conn, 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())
                 {
                     Repository<string, ClientPool>.GetById(x._conn)._websocket.Controller(v.Controller).Publish(v);
                 }
             });
             x._websocket.OnDisconnected += (sender, args) => Repository<string, ClientPool>.Remove(x._conn);                    
             Repository<string, ClientPool>.AddOrUpdate(conn, x);
         }
     }
     return Repository<string, ClientPool>.GetById(conn);
 }
示例#2
0
        public static ClientPool GetInstance(string conn, string origin)
        {
            lock (Locker)
            {
                //Safety, remove disconnected instances
                Repository <string, ClientPool> .Remove(p => !p._websocket.Socket.Connected);

                if (!Repository <string, ClientPool> .ContainsKey(conn))
                {
                    var x = new ClientPool
                    {
                        _conn           = conn,
                        _textQueue      = new BlockingCollection <IMessage>(),
                        _jsonSerializer = new XSocketJsonSerializer()
                    };
                    x._websocket = new XSocketClient(x._conn, 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);

                    x._websocket.Open();
                    Repository <string, ClientPool> .AddOrUpdate(conn, x);
                }
            }
            return(Repository <string, ClientPool> .GetById(conn));
        }