private void AttachWebSocket(object sender, WebSocketConnectedEventArgs eventArgs)
        {
            // Accept each URI at the moment.
            eventArgs.IsHandled = true;

            AttachWebSocket(eventArgs.WebSocketClientSession);
        }
Пример #2
0
        private void AttachWebSocket(object sender, WebSocketConnectedEventArgs e)
        {
            // Accept each URI at the moment.
            e.IsHandled = true;

            e.WebSocketClientSession.MessageReceived += OnWebSocketMessageReceived;
            e.WebSocketClientSession.Closed          += (_, __) => e.WebSocketClientSession.MessageReceived -= OnWebSocketMessageReceived;
        }
        protected virtual void ClientConnected(object sender, WebSocketConnectedEventArgs e)
        {
            traceLogger.Trace("Socket连接完成通知:" + e.IsSuccess.ToString());
            if (!e.IsSuccess)
            {
                if (retryCount >= MaxRetryCount)
                {
                    //重试失败
                    traceLogger.Trace("重试失败");
                    onConnected(false);

                    retryCount = 0;
                }
                else
                {
                    retryCount++;
                    traceLogger.Trace("重试:{0}", retryCount);
                    Connect(true);
                }
            }
            else
            {
                Session.Session = client.Session;
                if (Session.Session is WebSocketClientSession)
                {
                    WebSocketClientSession clientSession = Session.Session as WebSocketClientSession;
                    clientSession.RemoteIP   = IP;
                    clientSession.RemotePort = Port;
                }

                // 一定不要在Socket数据接收主线程中使用同步发送命令的方法
                ThreadPool.QueueUserWorkItem(new WaitCallback((o) =>
                {
                    try
                    {
                        traceLogger.Trace("开始OnBeforeConnected");
                        IsConnected = OnBeforeConnected();
                        traceLogger.Trace("连接状态:{0}", IsConnected);
                    }
                    catch (Exception ex)
                    {
                        logger.Error("初始化连接出错:{0}", ex.ToString());
                    }

                    if (!IsConnected)
                    {
                        traceLogger.Trace("连接失败,关闭基础连接");
                        Close();
                    }

                    onConnected(IsConnected);
                }), null);


                clearRequestCommands();
            }
        }
Пример #4
0
        private void HandleConnection(object sender, WebSocketConnectedEventArgs e)
        {
            var wsc  = e.Connection;
            var path = wsc.Handshake.ResourceName.ToString(); // TODO test if this is rigth with the new server

            if (_types.ContainsKey(path))
            {
                //var type = typeof(WebSocket).MakeGenericType(_types[path]);
                var userSocket = (WebSocket)Activator.CreateInstance(_types[path]);
                userSocket.Connection = wsc;
                _userSockets.Add(wsc, userSocket);

                userSocket.Connected(wsc.Handshake);

                wsc.OnDisconnect += new EventHandler <DisconnectedEventArgs>(HandleDisconnect);
                wsc.OnReceive    += new EventHandler <DataReceivedEventArgs>(HandleReceive);
            }
        }
Пример #5
0
        private void UpgradeToWebSocketSession(UpgradedToWebSocketSessionEventArgs eventArgs)
        {
            _httpClientSession = null;

            _webSocketClientSession         = new WebSocketClientSession(_clientSocket, _log);
            _webSocketClientSession.Closed += OnWebSocketClientSessionClosed;

            var webSocketConnectedEventArgs = new WebSocketConnectedEventArgs(eventArgs.HttpRequest, _webSocketClientSession);

            try
            {
                WebSocketConnected?.Invoke(this, webSocketConnectedEventArgs);
            }
            finally
            {
                if (!webSocketConnectedEventArgs.IsHandled)
                {
                    _cancellationTokenSource.Cancel();
                }
            }
        }
Пример #6
0
 private void HandleWebSocketConnected(object sender, WebSocketConnectedEventArgs eventArgs)
 {
     WebSocketConnected?.Invoke(this, eventArgs);
 }