#region Send private async Task Send(ClientWebSocket webSocket) { try { NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Begins", true); // while (webSocket.State == WebSocketState.Open) if (webSocket.State == WebSocketState.Open) { //Console.WriteLine("Write some to send over to server.."); //string stringtoSend = Console.ReadLine(); string stringtoSend = txtOutgoing.Text; byte[] buffer = encoding.GetBytes(stringtoSend); await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, false, CancellationToken.None); // Console.WriteLine("Sent: " + stringtoSend); await Task.Delay(1000); } } catch (Exception ex) { NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Failed: " + ex.Message.ToString() + "", true); } NeSTCommonClass.WriteTextFile(LogPath + Logfilename, "Client:Sending to Server Process Ends", true); } #endregion
byte[] receiveBuffer = new byte[1024 * 10]; WebSocketReceiveResult receiveResult = await webSocket.ReceiveAsync(new ArraySegment<byte>(receiveBuffer), CancellationToken.None); var ServerstrReceived = System.Text.Encoding.Default.GetString(receiveBuffer); txtIncoming.Text += Environment.NewLine + "Client:" + ServerstrReceived.ToString(); //Added to send to Client from Server //string stringtoSend = txtOutgoing.Text; //byte[] buffer = encoding.GetBytes(stringtoSend); //await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Binary, false, CancellationToken.None); //Added to send to Client if (receiveResult.MessageType == WebSocketMessageType.Close) await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);