/// <summary> /// Event fired when a data is received from the Alchemy Websockets server instance. /// Parses data as JSON and calls the appropriate message or sends an error message. /// </summary> /// <param name="context">The user's connection context</param> public static void OnReceive(IWebSocketConnection socket, string message) { Console.WriteLine("Received Data From :" + socket.ClientAddressAndPort()); try { try { string appName = ""; if (applicationConnections.ContainsKey(socket.ClientAddressAndPort())) { appName = applicationConnections[socket.ClientAddressAndPort()]; foreach (var ConnectToApplication in TPService.Instance.ConnectToApplications) { ConnectToApplication.WebSocketSendMessageEvent -= OnWebSocketSendMessageEvent; ConnectToApplication.WebSocketSendMessageEvent += OnWebSocketSendMessageEvent; ConnectToApplication.ProcessWebSocketMessage(appName, message, socket.ClientAddressAndPort()); } } else { if (isConnectToCommand(socket, message, out appName)) { TpsLogManager <WebSocketPipe> .Info("New connection added for " + appName + " on " + socket.ClientAddressAndPort()); Console.WriteLine("New connection added for " + appName + " on " + socket.ClientAddressAndPort()); foreach (var ConnectToApplication in TPService.Instance.ConnectToApplications) { ConnectToApplication.WebSocketSendMessageEvent -= OnWebSocketSendMessageEvent; ConnectToApplication.WebSocketSendMessageEvent += OnWebSocketSendMessageEvent; ConnectToApplication.ProcessWebSocketMessage(appName, message, socket.ClientAddressAndPort()); } } else { Console.WriteLine("Unsuccessful attempt by " + socket.ClientAddressAndPort() + " : " + message); socket.Send(JsonConvert.SerializeObject(new { Error = " Invalid json message" })); } } } catch (Exception e) { TpsLogManager <WebSocketPipe> .Error("Error OnReceive : " + e.Message + e.StackTrace); socket.Send(JsonConvert.SerializeObject(new { Error = e.Message })); } } catch (Exception ex) { TpsLogManager <WebSocketPipe> .Error("Error on WebSocketPipe.OnReceive : " + ex.Message + ex.StackTrace, ex); socket.Send(JsonConvert.SerializeObject(new { Error = ex.Message })); } }
public void OnConnect(IWebSocketConnection socket) { try { webSocketConnections.TryAdd(socket.ClientAddressAndPort(), socket); Console.WriteLine("New websocket connection added for browser :" + socket.ClientAddressAndPort()); TpsLogManager <WebSocketPipe> .Info("New websocket connection added for browser : " + socket.ClientAddressAndPort()); } catch (Exception ex) { TpsLogManager <WebSocketPipe> .Error("Error on WebSocketPipe.OnConnect : " + ex.Message + ex.StackTrace); } }
private void DisconnectWebsocket(IWebSocketConnection socket) { string applicationName = ""; if (applicationConnections.ContainsKey(socket.ClientAddressAndPort())) { applicationName = applicationConnections[socket.ClientAddressAndPort()]; applicationConnections.TryRemove(socket.ClientAddressAndPort(), out applicationName); TpsLogManager <WebSocketPipe> .Info("Connection removed for " + applicationName + " on " + socket.ClientAddressAndPort()); Console.WriteLine("connection removed for " + applicationName + " on " + socket.ClientAddressAndPort()); } webSocketConnections.TryRemove(socket.ClientAddressAndPort(), out socket); Console.WriteLine("Websocket client disconnected : " + socket.ClientAddressAndPort()); TpsLogManager <WebSocketPipe> .Info("Websocket client disconnected : " + socket.ClientAddressAndPort()); }
private static bool isConnectToCommand(IWebSocketConnection socket, string message, out string applicationName) { applicationName = ""; #region Ignore invalid json message if (!(message.StartsWith("{") && message.EndsWith("}"))) { TpsLogManager <WebSocketPipe> .Error("Error OnReceive - Invalid json message " + message + " received from " + socket.ClientAddressAndPort()); socket.Send(JsonConvert.SerializeObject(new { Error = " Invalid json message" })); return(false); } #endregion try { JObject request = new JObject(); request = JObject.Parse(message); if (request.First == null) { TpsLogManager <WebSocketPipe> .Error("Error OnReceive - Invalid json message " + message + " received from " + socket.ClientAddressAndPort()); socket.Send(JsonConvert.SerializeObject(new { Error = " Invalid json message" })); return(false); } string command = ((Newtonsoft.Json.Linq.JProperty)(request.First)).Name; #region Ignore invalid command if (String.IsNullOrEmpty(command)) { TpsLogManager <WebSocketPipe> .Error("Error OnReceive - Invalid json message " + message + " received from " + socket.ClientAddressAndPort()); socket.Send(JsonConvert.SerializeObject(new { Error = " Invalid json message" })); return(false); } #endregion if (command == "connecttoapplication") { foreach (var x in request) { string name = x.Key; JToken value = x.Value; applicationName = value.Value <String>("appname"); } applicationConnections.TryAdd(socket.ClientAddressAndPort(), applicationName); return(true); } return(false); } catch (Exception e) { if (String.IsNullOrEmpty(message)) { TpsLogManager <WebSocketPipe> .Error("Error OnReceive : Null or empty messsage is received from " + socket.ClientAddressAndPort()); socket.Send(JsonConvert.SerializeObject(new { Error = " Invalid json message" })); return(false); } TpsLogManager <WebSocketPipe> .Error("Error OnReceive for " + socket.ClientAddressAndPort() + ": " + e.Message + e.StackTrace); socket.Send(JsonConvert.SerializeObject(new { Error = e.Message })); return(false); } }
public void OnDisconnect(IWebSocketConnection socket) { try { DisconnectWebsocket(socket); } catch (Exception ex) { TpsLogManager <WebSocketPipe> .Error("Error on WebSocketPipe.OnDisconnect " + socket.ClientAddressAndPort() + " : " + ex.Message + ex.StackTrace, ex); } }