コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="conn">server location ws://server</param>
        /// <param name="port">the port to use, for example 8080</param>
        /// <param name="origin">the origin of this clients, http://example.com</param>
        /// <param name="querystringParameters">Parameters to pass in with the connection</param>
        /// <returns></returns>
        public static ClientPool GetInstance(string conn, string port, string origin, IDictionary <string, string> querystringParameters = null)
        {
            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,
                        _querystringParameters = querystringParameters,
                        _textQueue             = new BlockingCollection <IMessage>(),
                        _jsonSerializer        = new XSocketJsonSerializer()
                    };
                    x._websocket = new XSocketClient($"{x._conn}:{x._port}", origin);
                    if (x._querystringParameters != null)
                    {
                        foreach (var q in x._querystringParameters)
                        {
                            x._websocket.QueryString.Add(q.Key, q.Value);
                        }
                    }
                    ((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 + x._port)._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 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));
        }