virtual protected void multiMsgBtn_Click(object sender, EventArgs e) { MultiMessage m = new MultiMessage(this.userID); m.Show(); this.Hide(); }
/// <summary> /// When the json string is successfully parsed will return with an IServerMessage implementation. /// </summary> public static IServerMessage Parse(IJsonEncoder encoder, string json) { // Nothing to parse? if (string.IsNullOrEmpty(json)) { HTTPManager.Logger.Error("MessageFactory", "Parse - called with empty or null string!"); return(null); } // We don't have to do further decoding, if it's an empty json object, then it's a KeepAlive message from the server if (json.Length == 2 && json == "{}") { return(new KeepAliveMessage()); } IDictionary <string, object> msg = null; try { // try to decode the json message with the encoder msg = encoder.DecodeMessage(json); } catch (Exception ex) { HTTPManager.Logger.Exception("MessageFactory", "Parse - encoder.DecodeMessage", ex); return(null); } if (msg == null) { HTTPManager.Logger.Error("MessageFactory", "Parse - Json Decode failed for json string: \"" + json + "\""); return(null); } // "C" is for message id IServerMessage result = null; if (!msg.ContainsKey("C")) { // If there are no ErrorMessage in the object, then it was a success if (!msg.ContainsKey("E")) { result = new ResultMessage(); } else { result = new FailureMessage(); } } else { result = new MultiMessage(); } result.Parse(msg); return(result); }
/// <summary> /// Closes the connection and shuts down the transport. /// </summary> public void Close() { if (this.State == ConnectionStates.Closed) { return; } this.State = ConnectionStates.Closed; //ReconnectStartedAt = null; ReconnectStarted = false; TransportConnectionStartedAt = null; if (Transport != null) { Transport.Abort(); Transport = null; } NegotiationResult = null; HTTPManager.Heartbeats.Unsubscribe(this); LastReceivedMessage = null; if (Hubs != null) { for (int i = 0; i < Hubs.Length; ++i) { (Hubs[i] as IHub).Close(); } } if (BufferedMessages != null) { BufferedMessages.Clear(); BufferedMessages = null; } if (OnClosed != null) { try { OnClosed(this); } catch (Exception ex) { HTTPManager.Logger.Exception("SignalR Connection", "OnClosed", ex); } } }
private void OnPollRequestFinished(HTTPRequest req, HTTPResponse resp) { if (req.State == HTTPRequestStates.Aborted) { HTTPManager.Logger.Warning("Transport - " + base.Name, "Poll - Request Aborted!"); } else { pollRequest = null; string text = string.Empty; switch (req.State) { case HTTPRequestStates.Finished: if (resp.IsSuccess) { HTTPManager.Logger.Information("Transport - " + base.Name, "Poll - Request Finished Successfully! " + resp.DataAsText); IServerMessage serverMessage = TransportBase.Parse(base.Connection.JsonEncoder, resp.DataAsText); if (serverMessage != null) { base.Connection.OnMessage(serverMessage); MultiMessage multiMessage = serverMessage as MultiMessage; if (multiMessage != null && multiMessage.PollDelay.HasValue) { PollDelay = multiMessage.PollDelay.Value; } LastPoll = DateTime.UtcNow; } } else { text = $"Poll - Request Finished Successfully, but the server sent an error. Status Code: {resp.StatusCode}-{resp.Message} Message: {resp.DataAsText}"; } break; case HTTPRequestStates.Error: text = "Poll - Request Finished with Error! " + ((req.Exception == null) ? "No Exception" : (req.Exception.Message + "\n" + req.Exception.StackTrace)); break; case HTTPRequestStates.ConnectionTimedOut: text = "Poll - Connection Timed Out!"; break; case HTTPRequestStates.TimedOut: text = "Poll - Processing the request Timed Out!"; break; } if (!string.IsNullOrEmpty(text)) { base.Connection.Error(text); } } }
private void OnConnectRequestFinished(HTTPRequest req, HTTPResponse resp) { string str = string.Empty; switch (req.State) { case HTTPRequestStates.Finished: { if (!resp.IsSuccess) { str = $"Connect - Request Finished Successfully, but the server sent an error. Status Code: {resp.StatusCode}-{resp.Message} Message: {resp.DataAsText}"; break; } HTTPManager.Logger.Information("Transport - " + base.Name, "Connect - Request Finished Successfully! " + resp.DataAsText); base.OnConnected(); IServerMessage msg = TransportBase.Parse(base.Connection.JsonEncoder, resp.DataAsText); if (msg != null) { base.Connection.OnMessage(msg); MultiMessage message2 = msg as MultiMessage; if ((message2 != null) && message2.PollDelay.HasValue) { this.PollDelay = message2.PollDelay.Value; } } break; } case HTTPRequestStates.Error: str = "Connect - Request Finished with Error! " + ((req.Exception == null) ? "No Exception" : (req.Exception.Message + "\n" + req.Exception.StackTrace)); break; case HTTPRequestStates.Aborted: str = "Connect - Request Aborted!"; break; case HTTPRequestStates.ConnectionTimedOut: str = "Connect - Connection Timed Out!"; break; case HTTPRequestStates.TimedOut: str = "Connect - Processing the request Timed Out!"; break; } if (!string.IsNullOrEmpty(str)) { base.Connection.Error(str); } }
/// <summary> /// Called when we receive a message from the server /// </summary> void IConnection.OnMessage(IServerMessage msg) { if (this.State == ConnectionStates.Closed) { return; } // Store messages that we receive while we are connecting if (this.State == ConnectionStates.Connecting) { if (BufferedMessages == null) { BufferedMessages = new List <IServerMessage>(); } BufferedMessages.Add(msg); return; } LastMessageReceivedAt = DateTime.UtcNow; switch (msg.Type) { case MessageTypes.Multiple: LastReceivedMessage = msg as MultiMessage; // Not received in the reconnect process, so we can't rely on it if (LastReceivedMessage.IsInitialization) { HTTPManager.Logger.Information("SignalR Connection", "OnMessage - Init"); } if (LastReceivedMessage.GroupsToken != null) { GroupsToken = LastReceivedMessage.GroupsToken; } if (LastReceivedMessage.ShouldReconnect) { HTTPManager.Logger.Information("SignalR Connection", "OnMessage - Should Reconnect"); Reconnect(); // Should we return here not processing the messages that may come with it? //return; } if (LastReceivedMessage.Data != null) { for (int i = 0; i < LastReceivedMessage.Data.Count; ++i) { (this as IConnection).OnMessage(LastReceivedMessage.Data[i]); } } break; case MessageTypes.MethodCall: MethodCallMessage methodCall = msg as MethodCallMessage; Hub hub = this[methodCall.Hub]; if (hub != null) { (hub as IHub).OnMethod(methodCall); } else { HTTPManager.Logger.Warning("SignalR Connection", string.Format("Hub \"{0}\" not found!", methodCall.Hub)); } break; case MessageTypes.Result: case MessageTypes.Failure: case MessageTypes.Progress: UInt64 id = (msg as IHubMessage).InvocationId; hub = FindHub(id); if (hub != null) { (hub as IHub).OnMessage(msg); } else { HTTPManager.Logger.Warning("SignalR Connection", string.Format("No Hub found for Progress message! Id: {0}", id.ToString())); } break; case MessageTypes.Data: if (OnNonHubMessage != null) { OnNonHubMessage(this, (msg as DataMessage).Data); } break; case MessageTypes.KeepAlive: break; default: HTTPManager.Logger.Warning("SignalR Connection", "Unknown message type received: " + msg.Type.ToString()); break; } }
void OnPollRequestFinished(HTTPRequest req, HTTPResponse resp) { // When Stop() called on the transport. // In Stop() we set the pollRequest to null, but a new poll request can be made after a quick reconnection, and there is a chanse that // in this handler function we can null out the new request. So we return early here. if (req.State == HTTPRequestStates.Aborted) { HTTPManager.Logger.Warning("Transport - " + this.Name, "Poll - Request Aborted!"); return; } // Set the pollRequest to null, now we can send out a new one pollRequest = null; // error reason if there is any. We will call the manager's Error function if it's not empty. string reason = string.Empty; switch (req.State) { // The request finished without any problem. case HTTPRequestStates.Finished: if (resp.IsSuccess) { HTTPManager.Logger.Information("Transport - " + this.Name, "Poll - Request Finished Successfully! " + resp.DataAsText); IServerMessage msg = TransportBase.Parse(Connection.JsonEncoder, resp.DataAsText); if (msg != null) { Connection.OnMessage(msg); MultiMessage multiple = msg as MultiMessage; if (multiple != null && multiple.PollDelay.HasValue) { PollDelay = multiple.PollDelay.Value; } LastPoll = DateTime.UtcNow; } } else { reason = string.Format("Poll - Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}", resp.StatusCode, resp.Message, resp.DataAsText); } break; // The request finished with an unexpected error. The request's Exception property may contain more info about the error. case HTTPRequestStates.Error: reason = "Poll - Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"); break; // Ceonnecting to the server is timed out. case HTTPRequestStates.ConnectionTimedOut: reason = "Poll - Connection Timed Out!"; break; // The request didn't finished in the given time. case HTTPRequestStates.TimedOut: reason = "Poll - Processing the request Timed Out!"; break; } if (!string.IsNullOrEmpty(reason)) { Connection.Error(reason); } }
void OnConnectRequestFinished(HTTPRequest req, HTTPResponse resp) { // error reason if there is any. We will call the manager's Error function if it's not empty. string reason = string.Empty; switch (req.State) { // The request finished without any problem. case HTTPRequestStates.Finished: if (resp.IsSuccess) { HTTPManager.Logger.Information("Transport - " + this.Name, "Connect - Request Finished Successfully! " + resp.DataAsText); OnConnected(); IServerMessage msg = TransportBase.Parse(Connection.JsonEncoder, resp.DataAsText); if (msg != null) { Connection.OnMessage(msg); MultiMessage multiple = msg as MultiMessage; if (multiple != null && multiple.PollDelay.HasValue) { PollDelay = multiple.PollDelay.Value; } } } else { reason = string.Format("Connect - Request Finished Successfully, but the server sent an error. Status Code: {0}-{1} Message: {2}", resp.StatusCode, resp.Message, resp.DataAsText); } break; // The request finished with an unexpected error. The request's Exception property may contain more info about the error. case HTTPRequestStates.Error: reason = "Connect - Request Finished with Error! " + (req.Exception != null ? (req.Exception.Message + "\n" + req.Exception.StackTrace) : "No Exception"); break; // The request aborted, initiated by the user. case HTTPRequestStates.Aborted: reason = "Connect - Request Aborted!"; break; // Ceonnecting to the server is timed out. case HTTPRequestStates.ConnectionTimedOut: reason = "Connect - Connection Timed Out!"; break; // The request didn't finished in the given time. case HTTPRequestStates.TimedOut: reason = "Connect - Processing the request Timed Out!"; break; } if (!string.IsNullOrEmpty(reason)) { Connection.Error(reason); } }
void IConnection.OnMessage(IServerMessage msg) { if (State != ConnectionStates.Closed) { if (State == ConnectionStates.Connecting) { if (BufferedMessages == null) { BufferedMessages = new List <IServerMessage>(); } BufferedMessages.Add(msg); } else { LastMessageReceivedAt = DateTime.UtcNow; switch (msg.Type) { case MessageTypes.KeepAlive: break; case MessageTypes.Multiple: LastReceivedMessage = (msg as MultiMessage); if (LastReceivedMessage.IsInitialization) { HTTPManager.Logger.Information("SignalR Connection", "OnMessage - Init"); } if (LastReceivedMessage.GroupsToken != null) { GroupsToken = LastReceivedMessage.GroupsToken; } if (LastReceivedMessage.ShouldReconnect) { HTTPManager.Logger.Information("SignalR Connection", "OnMessage - Should Reconnect"); Reconnect(); } if (LastReceivedMessage.Data != null) { for (int i = 0; i < LastReceivedMessage.Data.Count; i++) { ((IConnection)this).OnMessage(LastReceivedMessage.Data[i]); } } break; case MessageTypes.MethodCall: { MethodCallMessage methodCallMessage = msg as MethodCallMessage; Hub hub = this[methodCallMessage.Hub]; if (hub != null) { ((IHub)hub).OnMethod(methodCallMessage); } else { HTTPManager.Logger.Warning("SignalR Connection", $"Hub \"{methodCallMessage.Hub}\" not found!"); } break; } case MessageTypes.Result: case MessageTypes.Failure: case MessageTypes.Progress: { ulong invocationId = (msg as IHubMessage).InvocationId; Hub hub = FindHub(invocationId); if (hub != null) { ((IHub)hub).OnMessage(msg); } else { HTTPManager.Logger.Warning("SignalR Connection", $"No Hub found for Progress message! Id: {invocationId.ToString()}"); } break; } case MessageTypes.Data: if (this.OnNonHubMessage != null) { this.OnNonHubMessage(this, (msg as DataMessage).Data); } break; default: HTTPManager.Logger.Warning("SignalR Connection", "Unknown message type received: " + msg.Type.ToString()); break; } } } }