public void SendToClient(MsgCode code, string body) { var text = MsgEncoding.Encode(code, body); Console.WriteLine("Sent " + text); swSender.WriteLine(text); swSender.Flush(); }
private void AcceptClient() { UserName = MsgEncoding.Decode(srReceiver.ReadLine()).Body; if (!ValidateUserName()) { return; } SendToClient(MsgCode.ConnectionSuccess, $"{IpAddress}:{Port}"); ChatServer.IncludeUser(this); KeepListening(); }
private void KeepListening() { try { while (true) { strAnswer = srReceiver.ReadLine(); Console.WriteLine("Received " + strAnswer); if (strAnswer == null) { ChatServer.RemoveUser(this); break; } var answer = MsgEncoding.Decode(strAnswer); switch (answer.Code) { case MsgCode.GlobalChat: var message = answer.Body; ChatServer.SendMessage(UserName, message); break; case MsgCode.RequestConnection: ChatServer.GiveConnectionInfo(answer.Body); break; case MsgCode.OnlineListRequest: var r = ""; foreach (var item in ChatServer.Connections) { r += item.UserName + ":"; } SendToClient(MsgCode.OnlineListRequest, r); break; } } } catch (Exception) { ChatServer.RemoveUser(this); } }