/// <summary> /// Send a message to the server, asynchronously. /// </summary> /// <param name="data">Data to send to the server.</param> /// <returns>Task with Boolean indicating if the message was sent successfully.</returns> public async Task <bool> SendAsync(byte[] data) { if (Wtcp == null) { if (Debug) { Console.WriteLine("Client is null, cannot send"); } return(false); } if (Wtcp.IsConnected()) { await Wtcp.SendAsync(data); return(true); } else { if (Debug) { Console.WriteLine("Client is not connected, cannot send"); } return(false); } }
static void Main(string[] args) { Console.Write("Server IP : "); serverIp = Console.ReadLine(); Console.Write("Server Port : "); serverPort = Convert.ToInt32(Console.ReadLine()); Console.Write("Certificate File : "); certFile = Console.ReadLine(); Console.Write("Certificate Pass : "******"Command [? for help]: "); string userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { continue; } switch (userInput) { case "?": Console.WriteLine("Available commands:"); Console.WriteLine(" ? help (this menu)"); Console.WriteLine(" q quit"); Console.WriteLine(" cls clear screen"); Console.WriteLine(" send send message to server"); Console.WriteLine(" sendasync send message to server asynchronously"); Console.WriteLine(" status show if client connected"); Console.WriteLine(" dispose dispose of the connection"); Console.WriteLine(" connect connect to the server if not connected"); Console.WriteLine(" reconnect disconnect if connected, then reconnect"); break; case "q": runForever = false; break; case "cls": Console.Clear(); break; case "send": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } client.Send(Encoding.UTF8.GetBytes(userInput)); break; case "sendasync": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } client.SendAsync(Encoding.UTF8.GetBytes(userInput)); break; case "status": if (client == null) { Console.WriteLine("Connected: False (null)"); } else { Console.WriteLine("Connected: " + client.IsConnected()); } break; case "dispose": client.Dispose(); break; case "connect": if (client != null && client.IsConnected()) { Console.WriteLine("Already connected"); } else { client = new WatsonTcpSslClient(serverIp, serverPort, certFile, certPass, true, false, ServerConnected, ServerDisconnected, MessageReceived, true); } break; case "reconnect": if (client != null) { client.Dispose(); } client = new WatsonTcpSslClient(serverIp, serverPort, certFile, certPass, true, false, ServerConnected, ServerDisconnected, MessageReceived, true); break; default: break; } } }