SetCookie() public method

Sets an HTTP cookie to send with the WebSocket handshake request to the server.
This method is not available in a server.
public SetCookie ( Cookie cookie ) : void
cookie System.Net.Cookie /// A that represents a cookie to send. ///
return void
Esempio n. 1
0
        public override object ConstructObject()
        {
            string[] protocols;
            if (FProtocols[this.CurrObj][0] != "")
            {
                protocols = new string[FProtocols[this.CurrObj].SliceCount];
                for (int i = 0; i < FProtocols[this.CurrObj].SliceCount; i++)
                {
                    protocols[i] = FProtocols[this.CurrObj][i];
                }
            }
            else
            {
                protocols = new string[0];
            }

            WebSocketSharp.WebSocket newWebSocket       = new WebSocketSharp.WebSocket(FUrl[this.CurrObj], protocols);
            VebSocketHostedClient    newVebSocketClient = new VebSocketHostedClient(newWebSocket);

            newVebSocketClient.SubscribeToMainloop(this.FHDEHost);

            if (FOrigin[this.CurrObj] != "")
            {
                newWebSocket.Origin = FOrigin[this.CurrObj];
            }

            if (FCookie.IsConnected && (FCookie[this.CurrObj] != null))
            {
                newWebSocket.SetCookie(FCookie[this.CurrObj]);
            }

            if (FServerCertValidCallback.IsConnected && (FServerCertValidCallback[this.CurrObj] != null))
            {
                newWebSocket.SslConfiguration.ServerCertificateValidationCallback = FServerCertValidCallback[this.CurrObj];
            }

            if ((FUser[this.CurrObj] != "") || (FPassword[this.CurrObj] != ""))
            {
                newWebSocket.SetCredentials(FUser[this.CurrObj], FPassword[this.CurrObj], FPreAuth[this.CurrObj]);
            }

            if (FAutoConnect[this.CurrObj])
            {
                newWebSocket.ConnectAsync();
            }

            return(newVebSocketClient);
        }
Esempio n. 2
0
 public void BindAndAuthenticate(string token,
                         EventHandler OnOpen,
                         EventHandler<MessageEventArgs> OnMessage,
                         EventHandler<CloseEventArgs> OnClose,
                         EventHandler<ErrorEventArgs> OnError)
 {
     if (ws != null && ws.IsAlive)
     {
         ws.Close();
     }
     ws = new WebSocket(WebSocketServiceEndpointUrl);//ws://localhost:8080/?name=
     if (token == null) token = "";
     ws.SetCookie(new WebSocketSharp.Net.Cookie("token", token));
     ws.OnOpen += new EventHandler(OnOpen);
     ws.OnMessage += new EventHandler<MessageEventArgs>(OnMessage);
     ws.OnClose += new EventHandler<CloseEventArgs>(OnClose);
     ws.OnError += new EventHandler<ErrorEventArgs>(OnError);
     ws.Connect();
 }
        /// <summary>
        /// Initialize the web socket connection to the live data server
        /// </summary>
        public void Initialize()
        {
            _initialized = true;

            _builder = new UriBuilder(new Uri(_liveDataUrl)) { Port = _liveDataPort };
            _ws = new WebSocket(_builder.ToString());

            var connectionRetryAttempts = 0;

            var timeStamp = (int)Time.TimeStamp();
            var hash = Api.CreateSecureHash(timeStamp, _token);

            _ws.SetCookie(new Cookie("Timestamp", timeStamp.ToString()));
            _ws.SetCookie(new Cookie("hash", hash));
            _ws.SetCookie(new Cookie("uid", _userId.ToString()));

            // Message received from server
            _ws.OnMessage += (sender, e) =>
            {
                lock (_locker)
                {
                    IEnumerable<BaseData> baseDatas = new List<BaseData>();
                    try
                    {
                        baseDatas = BaseData.DeserializeMessage(e.Data);
                    }
                    catch
                    {
                        Log.Error("ApiWebSocketConnection.OnMessage(): An error was received from the server: {0}", e.Data);
                    }
                    
                    foreach (var baseData in baseDatas)
                    {
                        _baseDataFromServer.Enqueue(baseData);
                    }
                }
            };

            // Error has in web socket connection
            _ws.OnError += (sender, e) =>
            {
                Log.Error("WebSocketConnection.Initialize(): Web socket connection error: {0}", e.Message);
                if (!_ws.IsAlive && connectionRetryAttempts < MaxRetryAttempts)
                {
                    Log.Trace(
                        "WebSocketConnection.Initialize(): Attempting to reconnect {0}/{1}",
                        connectionRetryAttempts, MaxRetryAttempts);

                    connectionRetryAttempts++;
                    _ws.Connect();
                }
                else
                {
                    Log.Trace(
                        "WebSocketConnection.Initialize(): Could not reconnect to web socket server. " +
                        "Closing web socket.");

                    if (_updateSubscriptions != null) _updateSubscriptions -= UpdateSubscriptions;
                    _updateSubscriptions = null;
                }
            };

            // Connection was closed
            _ws.OnClose += (sender, e) =>
            {
                Log.Trace(
                    "WebSocketConnection.Initialize(): Web socket connection closed: {0}, {1}",
                    e.Code, e.Reason);

                if (!_ws.IsAlive && connectionRetryAttempts < MaxRetryAttempts && e.Code == (ushort)CloseStatusCode.Abnormal)
                {
                    Log.Error(
                        "WebSocketConnection.Initialize(): Web socket was closed abnormally. " +
                        "Attempting to reconnect {0}/{1}",
                        connectionRetryAttempts, MaxRetryAttempts);

                    connectionRetryAttempts++;
                    _ws.Connect();
                }
                else
                {
                    Log.Trace(
                        "WebSocketConnection.Initialize(): Could not reconnect to web socket server. " +
                        "Closing web socket.");

                    if (_updateSubscriptions != null) _updateSubscriptions -= UpdateSubscriptions;
                    _updateSubscriptions = null;
                }
            };

            // Connection opened
            _ws.OnOpen += (sender, e) =>
            {
                SendSubscription();
                connectionRetryAttempts = 0;
            };

            _updateSubscriptions += UpdateSubscriptions;

            _ws.Connect();
        }
        protected override void PreStart()
        {
            var self = Self;
            _socket = new WebSocket(_endpoint);
            _socket.OnOpen += (sender, args) =>
            {
                self.Tell(new ConnectionOpened());
            };

            _socket.OnMessage += (sender, args) =>
            {
                self.Tell(new MessageReceived(args));
            };

            _socket.OnError += (sender, args) =>
            {
                self.Tell(new ErrorOccurred(args.Message, args.Exception));
            };
            _socket.OnClose += (sender, args) =>
            {
                self.Tell(new ConnectionClosed());
            };

            _socket.SetCookie(new Cookie("ClientId", _clientId));
            _socket.ConnectAsync();
        }