private void ReceiveMessages() { strReceiver = new StreamReader(tcpServer.GetStream()); while (connected) { string answerString = strReceiver.ReadLine(); var answer = MsgEncoding.Decode(answerString); switch (answer.Code) { case MsgCode.ConnectionSuccess: Invoke(new UpdateCallBack(UpdateLog), new object[] { "Conectado com sucesso!" }); StartListener(answer.Body); break; case MsgCode.ConnectionError: string reason = $"Erro de conexão: {answer.Body}"; Invoke(new UpdateCallBack(CloseConnection), new object[] { reason }); break; case MsgCode.GlobalChat: Invoke(new UpdateCallBack(UpdateLog), new object[] { answer.Body }); break; case MsgCode.RequestConnection: Invoke(new UpdateCallBack(UpdateLog), new object[] { "Received Requested IP: " + answer.Body }); StartDirectConnection(answer.Body); break; case MsgCode.OnlineListRequest: UpdateOnlineList(answer.Body); break; } } }
private void AcceptClient() { //algum tipo de verificação? if (string.IsNullOrEmpty(UserName)) { UserName = MsgEncoding.Decode(srReceiver.ReadLine()).Body; if (Page != null) { Page.Text = UserName; } } Form.Connections.Add(this); KeepListening(); }
private void KeepListening() { try { while (true) { strAnswer = srReceiver.ReadLine(); if (strAnswer == null) { Form.Connections.Remove(this); CloseConnection(); break; } var answer = MsgEncoding.Decode(strAnswer); switch (answer.Code) { case MsgCode.GlobalChat: var message = answer.Body; ChatServer.SendMessage(UserName, message); break; case MsgCode.DirectChat: ShowDirectMessage(answer.Body); break; case MsgCode.RequestConnection: ChatServer.GiveConnectionInfo(answer.Body); break; } } } catch (Exception) { Form.Connections.Remove(this); CloseConnection(); } }