Ejemplo n.º 1
0
        /// <summary>
        ///     Connect to the server and start processing messages
        /// </summary>
        public void Connect()
        {
            _webSocket?.Dispose();

            _disconnectCalled = false;

            // get the connection info from the signalr host
            var connInf = SignalR.Negotiate(_httpHost, _hubs);

            // we only work with websockets
            if (!connInf.TryWebSockets)
            {
                throw new WebSocketException(WebSocketError.UnsupportedProtocol,
                                             "WebSocket Connections Not Supported By This Host");
            }

            _connectionToken = connInf.ConnectionToken;
            _connectionId    = connInf.ConnectionId;

            _webSocket = SignalR.Connect(_wsHost, _connectionToken, _hubs);

            HookupEvents();

            _webSocket.Connect();

            if (Debug)
            {
                WriteLine($"Connect Called, URL: {_rootHost}");
            }
        }
Ejemplo n.º 2
0
 public int InvokeHubMethod(string hubName, string hubMethod, params object[] parameters)
 {
     if (Debug)
     {
         WriteLine($"Invoking Server Hub Method, Name: {hubName}, Method: {hubMethod}");
     }
     return(SignalR.InvokeHubMethod(_webSocket, hubName, hubMethod, parameters));
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Shutdown / Close the socket connection
        /// </summary>
        public void Disconnect()
        {
            _disconnectCalled = true;
            SignalR.Abort(_httpHost, _connectionToken, _hubs);
            _webSocket?.Disconnect();

            if (Debug)
            {
                WriteLine("Disconnect Called");
            }
        }
Ejemplo n.º 4
0
        private void _webSocket_OnMessage(string message)
        {
            try
            {
                if (Debug)
                {
                    WriteLine($"New Message, Message: {message}");
                }

                _lastMessageTime = DateTime.Now;
                // check if this is a keep alive
                if (message == "{{}}")
                {
                    return;
                }
                if (message.Trim() == "")
                {
                    return;
                }

                var msg = JsonConvert.DeserializeObject <WsResponse>(message);
                if (msg.S != null && msg.S == 1)
                {
                    // this is an init message lets confirm
                    SignalR.Start(_httpHost, _connectionToken, _hubs);
                    return;
                }

                // record the related info
                _lastMessageId = msg.C;
                if (!string.IsNullOrEmpty(msg.G))
                {
                    _groupsToken = msg.G;
                }

                // invoke the event
                if (msg.M != null && msg.M.Any() || msg.R != null || !string.IsNullOrEmpty(msg.I) ||
                    !string.IsNullOrEmpty(msg.C))
                {
                    Task.Run(() => { OnNewMessage?.Invoke(msg); });
                }
            }
            catch (Exception e)
            {
                Console.WriteLine($"SignalR Message Error: {e.Message}");
            }
        }
Ejemplo n.º 5
0
        private void ConnectionMonitorCheck(object state)
        {
            if (_disposedValue || _disconnectCalled)
            {
                return;
            }
            if (_lastMessageTime.AddSeconds(30) > DateTime.Now)
            {
                return;
            }

            if (Debug)
            {
                WriteLine("Connection Timeout, Atempting Reconnect");
            }

            _webSocket.Dispose(false);
            _webSocket = SignalR.Reconnect(_wsHost, _connectionToken, _hubs, _lastMessageId, _groupsToken);

            HookupEvents();

            _webSocket.Connect();
        }