// Mostly deprecated. Use only for SELF sent messages. Seen by only the client this function is called in. private void ClientChat(string Msg) { if (Msg != "") { if (this.InvokeRequired) { try { this.Invoke(new Action <string>(ClientChat), new object[] { Msg }); } catch { return; } } else { ChatHistory.AppendText(Msg); } } }
// Sends chat to chatbox. Thread safe. public void Chat(string Message, string Entity) { if (InvokeRequired) { try { this.Invoke(new Action <string, string>(Chat), new object[] { Message, Entity }); } catch { } return; } if (Message != "") { if (ChatHistory.Text == "") { ChatHistory.AppendText("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + Entity + ": " + Message); } else { ChatHistory.AppendText("\n[" + DateTime.Now.ToString("HH:mm:ss") + "] " + Entity + ": " + Message); } } }
private void ClientProcesssing() { try { //подключение к серверу client = new Socket(SocketType.Stream, ProtocolType.Tcp); int port = 50505; int.TryParse(PortBox.Text, out port); client.Connect(new IPEndPoint(IPAddress.Parse(IPBox.Text), port)); client.SendTimeout = sendTimeout; //отправка запроса авторизации JObject request = new JObject(); request.Add("action", "authorization"); request.Add("name", NameBox.Text); request.Add("password", PasswordBox.Text); SendRequest(client, request); string resultString = ""; //чтение ответа if ((resultString = ReceiveRequest(client)).Length > 0) { JObject result = JObject.Parse(resultString); if (result.GetValue("result").ToString() == "success") { clientConnected = true; Invoke((MethodInvoker)(() => { MessagePanel.Enabled = true; Disconnect.Enabled = true; MessageTextBox.Focus(); })); bool disconnectFromServer = false; //работа клиента while (clientConnected) { //чтение данных if (client.Available >= 8 && (resultString = ReceiveRequest(client)).Length > 0) { result = JObject.Parse(resultString); string action = result.GetValue("action").ToString(); if (action == "message") { string text = result.GetValue("message").ToString(); Invoke((MethodInvoker)(() => { label1.Text = label1.Text + (text + Environment.NewLine); ChatHistory.AppendText(text + Environment.NewLine); })); } else if (action == "disconnect") { disconnectFromServer = true; clientConnected = false; } } } //запрос отключения if (!disconnectFromServer) { request = new JObject(); request.Add("action", "disconnect"); SendRequest(client, request); } DisconnectUpdateUI(); //отключение от сервера client.Close(); } else { client.Close(); DisconnectUpdateUI(); MessageBox.Show(result.GetValue("message").ToString()); } } else { client.Close(); DisconnectUpdateUI(); MessageBox.Show("Unable to connect!"); } } catch (ThreadAbortException) { } catch (Exception exception) { DisconnectUpdateUI(); MessageBox.Show(exception.Message); } }
private void appendOutputHermes() { ChatHistory.AppendText("Hermes: " + respuesta + "\r\n"); }
private void appendOutputUser() { ChatHistory.AppendText("Usted: " + EnterText.Text + "\r\n"); }