protected override void OnMessage(MessageEventArgs e) { var msg = e.Data; if (_echo) { Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(e.Data.Length < Console.WindowWidth ? e.Data : e.Data.Substring(0, Console.WindowWidth - 1)); Console.ResetColor(); } TCPCommand incomingCommand; JsonSerializer serializer = new JsonSerializer(); using (JsonTextReader reader = new JsonTextReader(new StringReader(e.Data))) { incomingCommand = serializer.Deserialize <TCPCommand>(reader); } TCPCommand response = _server.executeCommand(incomingCommand); StringWriter sw = new StringWriter(); serializer.Serialize(sw, response); Send(sw.ToString()); }
public void SendMessage(TCPCommand message) { foreach (WebSocketListener l in webSocketListeners) { if (l.SocketState() == WebSocketState.Open) { l.SendMessage(message); } } }
public void SendMessage(TCPCommand message) { messageQueue.Enqueue(message); sendSignal.Set(); /*JsonSerializer serializer = new JsonSerializer(); * StringWriter sw = new StringWriter(); * serializer.Serialize(sw, message); * Send(sw.ToString()); * sw.Dispose();*/ }
public TCPCommand executeCommand(TCPCommand cmd) { if (cmd == null) { // null command? Can't do anything with a null command! return(null); } else { try { MethodInfo method = FindMethod(cmd.name, _commandStructure.GetType()); if (method == null) { //tcpCommand response = new tcpCommand("__error", null); //response.guid = cmd.guid; //return response; if (cmd.name == "GetName") { TCPCommand resp = new TCPCommand("__response", new List <object> { _linkName }); resp.guid = cmd.guid; return(resp); } return(null); } else { object[] args; if (cmd.arguments != null) { args = cmd.arguments.ToArray(); } else { args = new object[0]; } int idx = 0; foreach (ParameterInfo param in method.GetParameters()) { if (args[idx] is Newtonsoft.Json.Linq.JArray) { Newtonsoft.Json.Linq.JArray jArr = (Newtonsoft.Json.Linq.JArray)args[idx]; args[idx] = jArr.ToObject(param.ParameterType); } else { args[idx] = Convert.ChangeType(args[idx], param.ParameterType); } idx++; } object retval = method.Invoke(_commandStructure, args); TCPCommand response = new TCPCommand("__response", new List <object> { retval }); response.guid = cmd.guid; return(response); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); TCPCommand response = new TCPCommand("__error", null); response.guid = cmd.guid; return(response); } } }