Example #1
0
        /// <summary>
        /// Protocol negotiation finished successfully.
        /// </summary>
        private void OnNegotiationDataReceived(NegotiationData data)
        {
#if !BESTHTTP_DISABLE_WEBSOCKET
            if (data.TryWebSockets)
            {
                Transport = new WebSocketTransport(this);

                #if !BESTHTTP_DISABLE_SERVERSENT_EVENTS
                NextProtocolToTry = SupportedProtocols.ServerSentEvents;
                #else
                NextProtocolToTry = SupportedProtocols.HTTP;
                #endif
            }
            else
#endif
            {
                #if !BESTHTTP_DISABLE_SERVERSENT_EVENTS
                Transport = new ServerSentEventsTransport(this);

                // Long-Poll
                NextProtocolToTry = SupportedProtocols.HTTP;
                #else
                Transport = new PollingTransport(this);

                NextProtocolToTry = SupportedProtocols.Unknown;
                #endif
            }

            this.State = ConnectionStates.Connecting;
            TransportConnectionStartedAt = DateTime.UtcNow;

            Transport.Connect();
        }
        /// <summary>
        /// This function will begin to open the Socket.IO connection by sending out the handshake request.
        /// If the Options' AutoConnect is true, it will be called automatically.
        /// </summary>
        public void Open()
        {
            if (State != States.Initial &&
                State != States.Closed &&
                State != States.Reconnecting)
            {
                return;
            }

            HTTPManager.Logger.Information("SocketManager", "Opening");

            ReconnectAt = DateTime.MinValue;

            switch (Options.ConnectWith)
            {
            case TransportTypes.Polling: Transport = new PollingTransport(this); break;

#if !BESTHTTP_DISABLE_WEBSOCKET
            case TransportTypes.WebSocket: Transport = new WebSocketTransport(this); break;
#endif
            }
            Transport.Open();


            (this as IManager).EmitEvent("connecting");

            State = States.Opening;

            ConnectionStarted = DateTime.UtcNow;

            HTTPManager.Heartbeats.Subscribe(this);

            // The root namespace will be opened by default
            GetSocket("/");
        }
Example #3
0
        void IManager.OnTransportError(ITransport trans, string err)
        {
            (this as IManager).EmitError(SocketIOErrors.Internal, err);

            if (trans.State == TransportStates.Connecting ||
                trans.State == TransportStates.Opening)
            {
#if !BESTHTTP_DISABLE_WEBSOCKET
                if (trans is WebSocketTransport)
                {
                    trans.Close();

                    // try to fall back
                    Transport = new PollingTransport(this);
                    Transport.Open();
                }
                else // fallback failed
#endif
                (this as IManager).TryToReconnect();
            }
            else
            {
                trans.Close();
                (this as IManager).TryToReconnect();
            }
        }
Example #4
0
        private bool TryFallbackTransport()
        {
            if (State == ConnectionStates.Connecting)
            {
                if (BufferedMessages != null)
                {
                    BufferedMessages.Clear();
                }
                Transport.Stop();
                Transport = null;
                switch (NextProtocolToTry)
                {
                case SupportedProtocols.ServerSentEvents:
                    Transport         = new ServerSentEventsTransport(this);
                    NextProtocolToTry = SupportedProtocols.HTTP;
                    break;

                case SupportedProtocols.HTTP:
                    Transport         = new PollingTransport(this);
                    NextProtocolToTry = SupportedProtocols.Unknown;
                    break;

                case SupportedProtocols.Unknown:
                    return(false);
                }
                TransportConnectionStartedAt = DateTime.UtcNow;
                Transport.Connect();
                if (PingRequest != null)
                {
                    PingRequest.Abort();
                }
                return(true);
            }
            return(false);
        }
Example #5
0
        /// <summary>
        /// Protocol negotiation finished successfully.
        /// </summary>
        private void OnNegotiationDataReceived(NegotiationData data)
        {
            // Find out what supported protocol the server speak
            int protocolIdx = -1;

            for (int i = 0; i < ClientProtocols.Length && protocolIdx == -1; ++i)
            {
                if (data.ProtocolVersion == ClientProtocols[i])
                {
                    protocolIdx = i;
                }
            }

            // No supported protocol found? Try using the latest one.
            if (protocolIdx == -1)
            {
                protocolIdx = (byte)ProtocolVersions.Protocol_2_2;
                HTTPManager.Logger.Warning("SignalR Connection", "Unknown protocol version: " + data.ProtocolVersion);
            }

            this.Protocol = (ProtocolVersions)protocolIdx;

#if !BESTHTTP_DISABLE_WEBSOCKET
            if (data.TryWebSockets)
            {
                Transport = new WebSocketTransport(this);

                #if !BESTHTTP_DISABLE_SERVERSENT_EVENTS
                NextProtocolToTry = SupportedProtocols.ServerSentEvents;
                #else
                NextProtocolToTry = SupportedProtocols.HTTP;
                #endif
            }
            else
#endif
            {
                #if !BESTHTTP_DISABLE_SERVERSENT_EVENTS
                Transport = new ServerSentEventsTransport(this);

                // Long-Poll
                NextProtocolToTry = SupportedProtocols.HTTP;
                #else
                Transport = new PollingTransport(this);

                NextProtocolToTry = SupportedProtocols.Unknown;
                #endif
            }

            this.State = ConnectionStates.Connecting;
            TransportConnectionStartedAt = DateTime.UtcNow;

            Transport.Connect();
        }
Example #6
0
 private void CreateTransports()
 {
     if (Handshake.Upgrades.Contains("websocket"))
     {
         Transport = new WebSocketTransport(this);
     }
     else
     {
         Transport = new PollingTransport(this);
     }
     Transport.Open();
 }
Example #7
0
        /// <summary>
        /// Try to fall back to next transport. If no more transport to try, it will return false.
        /// </summary>
        private bool TryFallbackTransport()
        {
            if (this.State == ConnectionStates.Connecting)
            {
                if (BufferedMessages != null)
                {
                    BufferedMessages.Clear();
                }

                // stop the current transport
                Transport.Stop();
                Transport = null;

                switch (NextProtocolToTry)
                {
#if !BESTHTTP_DISABLE_WEBSOCKET
                case SupportedProtocols.WebSocket:
                    Transport = new WebSocketTransport(this);
                    break;
#endif

#if !BESTHTTP_DISABLE_SERVERSENT_EVENTS
                case SupportedProtocols.ServerSentEvents:
                    Transport         = new ServerSentEventsTransport(this);
                    NextProtocolToTry = SupportedProtocols.HTTP;
                    break;
#endif

                case SupportedProtocols.HTTP:
                    Transport         = new PollingTransport(this);
                    NextProtocolToTry = SupportedProtocols.Unknown;
                    break;

                case SupportedProtocols.Unknown:
                    return(false);
                }

                TransportConnectionStartedAt = DateTime.UtcNow;

                Transport.Connect();

                if (PingRequest != null)
                {
                    PingRequest.Abort();
                }

                return(true);
            }

            return(false);
        }
Example #8
0
        /// <summary>
        /// Creates and starts opening/upgrading process of the transports.
        /// </summary>
        private void CreateTransports()
        {
#if !BESTHTTP_DISABLE_WEBSOCKET
            bool hasWSSupport = Handshake.Upgrades.Contains("websocket");

            if (hasWSSupport)
            {
                Transport = new WebSocketTransport(this);
            }
            else
#endif
            Transport = new PollingTransport(this);

            Transport.Open();
        }
Example #9
0
 void IManager.Close(bool removeSockets)
 {
     if (this.State == SocketManager.States.Closed)
     {
         return;
     }
     HTTPManager.Logger.Information("SocketManager", "Closing");
     HTTPManager.Heartbeats.Unsubscribe(this);
     if (removeSockets)
     {
         while (this.Sockets.Count > 0)
         {
             ((ISocket)this.Sockets[this.Sockets.Count - 1]).Disconnect(removeSockets);
         }
     }
     else
     {
         for (int i = 0; i < this.Sockets.Count; i++)
         {
             ((ISocket)this.Sockets[i]).Disconnect(removeSockets);
         }
     }
     this.State         = SocketManager.States.Closed;
     this.LastHeartbeat = DateTime.MinValue;
     if (this.OfflinePackets != null)
     {
         this.OfflinePackets.Clear();
     }
     if (removeSockets)
     {
         this.Namespaces.Clear();
     }
     if (this.Handshake != null)
     {
         this.Handshake.Abort();
     }
     this.Handshake = null;
     if (this.Transport != null)
     {
         this.Transport.Close();
     }
     this.Transport = null;
     if (this.Poller != null)
     {
         this.Poller.Close();
     }
     this.Poller = null;
 }
Example #10
0
        private void CreateTransports()
        {
            bool flag = this.Handshake.Upgrades.Contains("websocket");

            if (flag)
            {
                this.Transport = new WebSocketTransport(this);
            }
            this.Poller = new PollingTransport(this);
            this.Poller.Open();
            if (this.Transport == null)
            {
                this.Transport = this.Poller;
            }
            else
            {
                this.Transport.Open();
            }
        }
Example #11
0
        /// <summary>
        /// Creates and starts opening/upgrading process of the transports.
        /// </summary>
        private void CreateTransports()
        {
            bool hasWSSupport = Handshake.Upgrades.Contains("websocket");

            if (hasWSSupport)
            {
                Transport = new WebSocketTransport(this);
            }

            Poller = new PollingTransport(this);
            Poller.Open();

            if (Transport == null)
            {
                Transport = Poller as ITransport;
            }
            else
            {
                Transport.Open();
            }
        }
Example #12
0
 void IManager.OnTransportError(ITransport trans, string err)
 {
     ((IManager)this).EmitError(SocketIOErrors.Internal, err);
     if (trans.State == TransportStates.Connecting || trans.State == TransportStates.Opening)
     {
         if (trans is WebSocketTransport)
         {
             trans.Close();
             Transport = new PollingTransport(this);
             Transport.Open();
         }
         else
         {
             ((IManager)this).TryToReconnect();
         }
     }
     else
     {
         trans.Close();
         ((IManager)this).TryToReconnect();
     }
 }
Example #13
0
        /// <summary>
        /// Closes this Socket.IO connection.
        /// </summary>
        void IManager.Close(bool removeSockets)
        {
            if (State == States.Closed)
            {
                return;
            }

            HTTPManager.Logger.Information("SocketManager", "Closing");

            HTTPManager.Heartbeats.Unsubscribe(this);

            // Disconnect the sockets. The Disconnect function will call the Remove function to remove it from the Sockets list.
            if (removeSockets)
            {
                while (Sockets.Count > 0)
                {
                    (Sockets[Sockets.Count - 1] as ISocket).Disconnect(removeSockets);
                }
            }
            else
            {
                for (int i = 0; i < Sockets.Count; ++i)
                {
                    (Sockets[i] as ISocket).Disconnect(removeSockets);
                }
            }

            // Set to Closed after Socket's Disconnect. This way we can send the disconnect events to the server.
            State = States.Closed;

            LastHeartbeat = DateTime.MinValue;

            if (OfflinePackets != null)
            {
                OfflinePackets.Clear();
            }

            // Remove the references from the dictionary too.
            if (removeSockets)
            {
                Namespaces.Clear();
            }

            if (Handshake != null)
            {
                Handshake.Abort();
            }
            Handshake = null;

            if (Transport != null)
            {
                Transport.Close();
            }
            Transport = null;

            if (Poller != null)
            {
                Poller.Close();
            }
            Poller = null;
        }