/// <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, _options.Serializer, _options.IgnoreCertErrors); // 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, _options); HookupEvents(); _webSocket.Connect(); if (_options.DebugMode) { WriteLine($"Connect Called, URL: {_rootHost}"); } }
/// <summary> /// Shutdown / Close the socket connection /// </summary> public void Disconnect() { _disconnectCalled = true; SignalR.Abort(_httpHost, _connectionToken, _hubs, _options.IgnoreCertErrors); _webSocket?.Disconnect(); if (_options.DebugMode) { WriteLine("Disconnect Called"); } }
public int InvokeHubMethod(string hubName, string hubMethod, params object[] parameters) { if (_webSocket.State != WebSocketState.Open) { throw new Exception("WebSocket must be fully open before invoking a method."); } if (_options.DebugMode) { WriteLine($"Invoking Server Hub Method, Name: {hubName}, Method: {hubMethod}"); } return(SignalR.InvokeHubMethod(_webSocket, hubName, hubMethod, _options.Serializer, parameters)); }
private void _webSocket_OnMessage(string message) { try { if (_options.DebugMode) { WriteLine($"New Message, Message: {message}"); } _lastMessageTime = DateTime.Now; // check if this is a keep alive if (message == "{{}}") { return; } if (message.Trim().Length == 0) { return; } var msg = JsonSerializer.Deserialize <WsResponse>(message); if (msg.S != null && msg.S == 1) { // this is an init message lets confirm SignalR.Start(_httpHost, _connectionToken, _hubs, _options.IgnoreCertErrors); return; } // record the related info _lastMessageId = msg.C; if (!string.IsNullOrEmpty(msg.G)) { _groupsToken = msg.G; } // invoke the event if (msg.M?.Count > 0 || 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}"); } }
private void ConnectionMonitorCheck(object state) { if (_disposedValue || _disconnectCalled) { return; } if (_lastMessageTime.AddSeconds(30) > DateTime.Now) { return; } if (_options.DebugMode) { WriteLine("Connection Timeout, Attempting Reconnect"); } _webSocket.Dispose(false); _webSocket = SignalR.Reconnect(_wsHost, _connectionToken, _hubs, _lastMessageId, _groupsToken, _options); HookupEvents(); _webSocket.Connect(); }