public static void Send(int clientid, Helpers.TcpChat message) { int receiver = (int)message.Data.GetValue("receiver"); string msg = (string)message.Data.GetValue("message"); Logging.Info("[Server] Redirecting Chat message from " + clientid + " to " + receiver); server.Send(receiver, new Helpers.TcpChat(msg, GetUser(clientid)).Serialize()); }
public static void Send(Helpers.TcpChat chatmsg) { if (string.IsNullOrEmpty((string)chatmsg.Data.GetValue("message"))) { Logging.Warn("[Message] Your chat message can't be empty!"); WindowManager.SpawnDialog("Your chat message can't be empty!", true, DialogWindow.DialogType.Warning); return; } Logging.Info($"[Message] {((Helpers.User)chatmsg.Data.GetValue("sender")).Username}: " + (string)chatmsg.Data.GetValue("message")); client.Send(chatmsg.Serialize()); if (chatMessages.Count == 18) { chatMessages.RemoveAt(0); } chatMessages.Add($"{((Helpers.User)chatmsg.Data.GetValue("sender")).Username}: {(string)chatmsg.Data.GetValue("message")}"); chatWindow.text = string.Join("\n", chatMessages); }
/// <summary> /// Gets called whenever a message (other than connect or disconnect) gets received /// </summary> /// <param name="msg">The Telepathy.Message sent by the server.getNextMessage() function</param> static void Receive(Telepathy.Message msg) { //string datastr = Encoding.UTF8.GetString(msg.data); //Logging.Info($"[Server] From Connection {msg.connectionId}: " + datastr); Logging.Info($"[Server] Data from Connection {msg.connectionId}: {msg.data.Length} bytes"); //Handle TCPLogin //Helpers.TcpLogin tcplogin = XML.From<Helpers.TcpLogin>(datastr); Helpers.TcpLogin tcplogin = Helpers.TcpLogin.Deserialize(msg.data); if (tcplogin != null && tcplogin.Header == "login") { OnUserLogin(msg.connectionId, tcplogin); } //Handle TCPChat //Helpers.TcpChat tcpchat = XML.From<Helpers.TcpChat>(datastr); Helpers.TcpChat tcpchat = Helpers.TcpChat.Deserialize(msg.data); if (tcpchat != null && tcpchat.Header == "chat") { OnUserChat(msg.connectionId, tcpchat); } //Handle TCPRequests //Helpers.TcpRequest tcprequest = XML.From<Helpers.TcpRequest>(datastr); Helpers.TcpRequest tcprequest = Helpers.TcpRequest.Deserialize(msg.data); if (tcprequest != null && tcprequest.Header == "request") { string req = (string)tcprequest.Data.GetValue("request"); if (req == "gameworld") { OnRequestGameWorld(msg.connectionId); } else if (req == "userlist") { OnRequestUserList(msg.connectionId); } } Helpers.TcpGamespeed tcpspeed = Helpers.TcpGamespeed.Deserialize(msg.data); if (tcpspeed != null && tcpspeed.Header == "gamespeed") { OnGamespeedChange(msg.connectionId, tcpspeed); } }
static void OnUserChat(int connectionid, Helpers.TcpChat chat) { if (chat.Data.GetValue("receiver") == null) { //Send to all connected users Logging.Info($"[Server] User {connectionid} sends a chat to all connected users"); foreach (Helpers.User u in Users) { if (u.ID != connectionid) { server.Send(u.ID, chat.Serialize()); } } } else { //Send to a receiver Logging.Info($"[Server] User {connectionid} sends a chat to {(int)chat.Data.GetValue("receiver")}"); server.Send((int)chat.Data.GetValue("receiver"), chat.Serialize()); } }
static void OnChatReceived(Helpers.TcpChat chat) { Helpers.User sender = (Helpers.User)chat.Data.GetValue("sender"); if (sender == null) { sender = new Helpers.User() { Username = "******" } } ; Logging.Info($"[Message] {sender.Username}: {(string)chat.Data.GetValue("message")}"); if (chatMessages.Count == 18) { chatMessages.RemoveAt(0); } chatMessages.Add($"{sender.Username}: {(string)chat.Data.GetValue("message")}"); chatLogMessages.Add($"[{sender.Username}] [{DateTime.Now.ToString()}] {(string)chat.Data.GetValue("message")}"); chatWindow.text = string.Join("\n", chatMessages); } }
static void Receive(byte[] data) { Logging.Info("[Client] Data from Server: " + data.Length + " bytes"); //Handle TcpResponse Helpers.TcpResponse tcpresponse = Helpers.TcpResponse.Deserialize(data); if (tcpresponse != null && tcpresponse.Header == "response") { OnServerResponse(tcpresponse); } //Handle TcpServerChat Helpers.TcpServerChat tcpServerChat = Helpers.TcpServerChat.Deserialize(data); if (tcpServerChat != null && tcpServerChat.Header == "serverchat") { OnServerChatRecieved(tcpServerChat); } //Handle TcpChat Helpers.TcpChat tcpchat = Helpers.TcpChat.Deserialize(data); if (tcpchat != null && tcpchat.Header == "chat") { OnChatReceived(tcpchat); } //Handle GameWorld Helpers.TcpGameWorld tcpworld = Helpers.TcpGameWorld.Deserialize(data); if (tcpworld != null && tcpworld.Header == "gameworld") { OnGameWorldReceived(tcpworld); } //Handle Gamespeed Helpers.TcpGamespeed tcpspeed = Helpers.TcpGamespeed.Deserialize(data); if (tcpspeed != null && tcpspeed.Header == "gamespeed") { OnGamespeedChange(tcpspeed); } }