public ChatConnectionResult Disconnect() { ChatConnectionResult result; try { Connected = false; Writer.Close(); TcpClient.Close(); Writer.Dispose(); TcpClient.Dispose(); Writer = null; TcpClient = null; result = new ChatConnectionResult(true); } catch (Exception ex) { result = new ChatConnectionResult(ex); } return(result); }
/// <summary> /// Send message to the server. /// </summary> /// <param name="message"></param> public ChatConnectionResult SendMessage(string message) { ChatConnectionResult result = null; try { if (!string.IsNullOrEmpty(message)) { Writer.WriteLine(message); Writer.Flush(); result = new ChatConnectionResult(true); } else { result = new ChatConnectionResult(false); } } catch (Exception ex) { result = new ChatConnectionResult(ex); } return(result); }
public ChatConnectionResult Connect(ChatUser user) { ChatConnectionResult result; try { // IP Address object based on the IP Address string IpAddress = IPAddress.Parse(IpAddressNumber); // Initiate a new TCP connection with the server TcpClient = new TcpClient(); TcpClient.Connect(IpAddressNumber, ServerChatServiceConstants.SERVER_PORT); // Property responsible to manage the connectivity Connected = true; // Send the nickname to the server Writer = new StreamWriter(TcpClient.GetStream()); Writer.WriteLine(user.Nickname); Writer.Flush(); // Starts a thread to receiving messages e new conversations ThreadMessage = new Thread(new ThreadStart(GetMessages)); ThreadMessage.Start(); Thread.Sleep(2000); result = new ChatConnectionResult(Connected, UserAlreadyTaken); } catch (Exception ex) { result = new ChatConnectionResult(ex); } return(result); }