Exemplo n.º 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);
 }
Exemplo n.º 2
0
 public static ClientPool GetInstance(string conn, string origin)
 {
     lock (Locker)
     {
         if (!Websockets.ContainsKey(conn))
         {
             var x = new ClientPool
             {
                 _conn = conn,
                 _textQueue = new BlockingCollection<ITextArgs>(),
                 _jsonSerializer = new XSocketJsonSerializer()
             };
             x._websocket = new XSocketClient(x._conn, origin);
             ((XSocketClient)x._websocket).OnSocketOpen += (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())
                 {
                     Websockets[x._conn]._websocket.Send(v);
                 }
             });
             x._websocket.OnClose += (sender, args) =>
             {
                 ClientPool v;
                 Websockets.TryRemove(x._conn, out v);
             };
             Websockets.TryAdd(conn, x);
             x._websocket.Open();
         }
     }
     return Websockets[conn];
 }
Exemplo n.º 3
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));
        }