private static void ConsoleSend(string _text) { if (_text == null || _text == "" || Clients.Count < 1) return; Data data; switch (_text.ToLower()) { case "!ping": double unixTime = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds; data = new Data { comm = Command.PING, data = unixTime.ToString() }; send_clients(JsonConvert.SerializeObject(data)); break; case "!kill": break; case "!show": showUsers(); break; default: data = new Data { comm = Command.MESSAGE, data = new { name = "Server", message = _text } }; send_clients(JsonConvert.SerializeObject(data)); break; } }
private static void ConsoleWrite(string _text) { if (_text == null || _text == "") return; Console.WriteLine(_text); if (Client_console != null) { Data data = new Data { comm = Command.CONSOLE_MESSAGE, data = _text }; send_client(JsonConvert.SerializeObject(data), Client_console); } }
private static void timerTick(object obj) { if (Clients.Count < 1) return; double unixTime = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds; Data data = new Data { comm = Command.PING, data = unixTime.ToString() }; send_clients(JsonConvert.SerializeObject(data)); }
private static void OnReceive(UserContext context) { string json = string.Empty; dynamic obj = null; try { json = context.DataFrame.ToString(); if(json == string.Empty || ! json.Contains("}")) return; obj = JsonConvert.DeserializeObject(json); if (obj == null) return; } catch (Exception e) { #if(DEBUG) ConsoleWrite(e.ToString()); #endif return; } #if(DEBUG) ConsoleWrite("Received Data : " + json); #endif Client client = getClient(context.ClientAddress); if (client == null) return; if (client.user == null && (int)obj.comm != (int)Command.CHANGE_NAME) { client.context.Dispose(); return; } double unixTime = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds; switch ((int)obj.comm) { case (int)Command.MESSAGE: if((string)obj.data.message == "console" && client.user.id == 1 && Client_console == null) { Client_console = client; break; } if (Client_console != null && client == Client_console) { ConsoleSend((string)obj.data.message); break; } if (unixTime - client.last_rec < 250) { if (++client.user.warnings > 3) { Data data; if (++client.user.errors > 3) { context.Dispose(); return; } else { data = new Data { comm = Command.MUTE, data = ((client.user.errors) * 10).ToString() }; send_client(JsonConvert.SerializeObject(data), client); client.user.warnings = 0; } } } else client.user.warnings = 0; client.last_rec = unixTime; send_clients(JsonConvert.SerializeObject(obj)); break; case (int)Command.MESSAGE_STATUS: send_clients(JsonConvert.SerializeObject(obj), UInt16.Parse((string)obj.data.id)); break; case (int)Command.PING: unixTime = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds; double unixLastTime = double.Parse((string)obj.data); client.ping = Convert.ToUInt16(unixTime - unixLastTime); break; case (int)Command.CHANGE_NAME: client.user = new User((string)obj.data.name, (ushort)obj.data.id); break; } }