protected override void OnReceived(byte[] buffer, long offset, long size) { string message = Encoding.UTF8.GetString(buffer, (int)offset, (int)size); // support to UTF8 only Logger.Log($"Receiving message, broadcasting it. Info: syze ({size}), message: {message}", this.Id); ChatServer.Broadcast(this, message); }
internal void Run() { while (true) { try { Mail mail = null; if (networkStream.CanWrite) { mail = (Mail) new BinaryFormatter().Deserialize(networkStream); // disconnect if (server.clients.ContainsValue(mail.Username) && mail.Msg.StartsWith("Bye")) { server.form.PrintLog($"{mail.Username}({tcpClient.Client.RemoteEndPoint}) leave the chat"); string[] s = mail.Msg.Split(','); var item = server.clients.First(kvp => kvp.Value == s[1]); item.Key.Close(); server.clients.Remove(item.Key); SendCurrentUserList(); } // new user if (!server.clients.ContainsValue(mail.Username) && mail.Msg.Equals("Hello")) { server.clients.Add(networkStream, (mail.Username)); server.form.PrintLog($"{mail.Username}({tcpClient.Client.RemoteEndPoint}) join the chat"); server.form.AddToDGV("id", mail.Username, tcpClient.Client.RemoteEndPoint.ToString()); SendCurrentUserList(); } else { server.Broadcast(mail); } } } catch (ArgumentException) { break; } }