當前位置: 首頁>>代碼示例>>C#>>正文


C# WebSocket.SetCookie方法代碼示例

本文整理匯總了C#中WebSocketSharp.WebSocket.SetCookie方法的典型用法代碼示例。如果您正苦於以下問題:C# WebSocket.SetCookie方法的具體用法?C# WebSocket.SetCookie怎麽用?C# WebSocket.SetCookie使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在WebSocketSharp.WebSocket的用法示例。


在下文中一共展示了WebSocket.SetCookie方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: BindAndAuthenticate

 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();
 }
開發者ID:XEonAX,項目名稱:Grind,代碼行數:19,代碼來源:WebsocketService.cs

示例2: Initialize

        /// <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;
            };

//.........這裏部分代碼省略.........
開發者ID:AlexCatarino,項目名稱:Lean,代碼行數:101,代碼來源:ApiWebSocketConnection.cs

示例3: PreStart

        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();
        }
開發者ID:christiansparre,項目名稱:IsThereAnybodyOutThere,代碼行數:26,代碼來源:WebSocketClientConnection.cs


注:本文中的WebSocketSharp.WebSocket.SetCookie方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。