protected async Task ConnectWebSocket() { var wsUrl = WebServerUrl.Replace("http", "ws") + _url; var wasSet = false; var clientSocket = new WebSocket(wsUrl); await clientSocket.ConnectAsync(); clientSocket.OnMessage += (s, e) => { Assert.AreEqual(e.Data, "HELLO"); wasSet = true; }; Assert.AreEqual( WebSocketState.Open, clientSocket.State, $"Connection should be open, but the status is {clientSocket.State} - {wsUrl}"); var buffer = System.Text.Encoding.UTF8.GetBytes("HOLA"); await clientSocket.SendAsync(buffer, Opcode.Text); await Task.Delay(TimeSpan.FromSeconds(1)); Assert.IsTrue(wasSet); }
public async Task TestWithDifferentCloseResponse() { var webSocketUrl = new Uri($"{WebServerUrl.Replace("http", "ws")}close"); using var clientSocket = new System.Net.WebSockets.ClientWebSocket(); await clientSocket.ConnectAsync(webSocketUrl, default).ConfigureAwait(false); var buffer = new ArraySegment <byte>(new byte[8192]); var result = await clientSocket.ReceiveAsync(buffer, default).ConfigureAwait(false); Assert.IsTrue(result.CloseStatus.HasValue); Assert.IsTrue(result.CloseStatus == System.Net.WebSockets.WebSocketCloseStatus.InvalidPayloadData); }
public async Task TestSendBigDataWebSocket() { var webSocketUrl = new Uri($"{WebServerUrl.Replace("http", "ws")}bigdata"); using var clientSocket = new System.Net.WebSockets.ClientWebSocket(); await clientSocket.ConnectAsync(webSocketUrl, default).ConfigureAwait(false); var buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes("HOLA")); await clientSocket.SendAsync(buffer, System.Net.WebSockets.WebSocketMessageType.Text, true, default).ConfigureAwait(false); var json = await ReadString(clientSocket).ConfigureAwait(false); Assert.AreEqual(Json.Serialize(BigDataWebSocket.BigDataObject), json); }
public async Task TestConnectWebSocket() { var websocketUrl = new Uri(WebServerUrl.Replace("http", "ws") + "test"); using var clientSocket = new System.Net.WebSockets.ClientWebSocket(); await clientSocket.ConnectAsync(websocketUrl, default); Assert.AreEqual( System.Net.WebSockets.WebSocketState.Open, clientSocket.State, $"Connection should be open, but the status is {clientSocket.State} - {websocketUrl}"); var buffer = new ArraySegment <byte>(Encoding.UTF8.GetBytes("HOLA")); await clientSocket.SendAsync(buffer, System.Net.WebSockets.WebSocketMessageType.Text, true, default); Assert.AreEqual(await ReadString(clientSocket), "HELLO"); }
public async Task TestConnectWebSocket() { var wsUrl = WebServerUrl.Replace("http", "ws") + "test"; Assert.IsNotNull(_webServer.Module <WebSocketsModule>(), "WebServer has WebSocketsModule"); Assert.AreEqual(_webServer.Module <WebSocketsModule>().Handlers.Count, 1, "WebSocketModule has one handler"); if (_ignoreWebConnect) { Assert.Inconclusive("WebSocket Connect not available"); } var ct = new CancellationTokenSource(); #if NET47 var clientSocket = new ClientWebSocket(); await clientSocket.ConnectAsync(new Uri(wsUrl), ct.Token); Assert.AreEqual(WebSocketState.Open, clientSocket.State, "Connection is open"); var message = new ArraySegment <byte>(System.Text.Encoding.Default.GetBytes("HOLA")); var buffer = new ArraySegment <byte>(new byte[5]); await clientSocket.SendAsync(message, WebSocketMessageType.Text, true, ct.Token); await clientSocket.ReceiveAsync(buffer, ct.Token); Assert.AreEqual("HELLO", System.Text.Encoding.UTF8.GetString(buffer.Array).Trim(), "Final message is HELLO"); #else var clientSocket = new WebSocket(wsUrl); await clientSocket.ConnectAsync(ct.Token); clientSocket.OnMessage += (s, e) => { Assert.AreEqual(e.Data, "HELLO"); }; Assert.AreEqual(WebSocketState.Open, clientSocket.State, "Connection is open"); var buffer = System.Text.Encoding.UTF8.GetBytes("HOLA"); await clientSocket.SendAsync(buffer, Opcode.Text, ct.Token); await Task.Delay(500, ct.Token); #endif }
public async Task TestSendBigDataWebSocket() { if (IgnoreWebConnect) { Assert.Inconclusive("WebSocket Connect not available"); } var webSocketUrl = WebServerUrl.Replace("http", "ws") + "bigdata"; var ct = new CancellationTokenSource(); #if NET47 var clientSocket = new ClientWebSocket(); await clientSocket.ConnectAsync(new Uri(webSocketUrl), ct.Token); Assert.AreEqual(WebSocketState.Open, clientSocket.State, "Connection is open"); var message = new ArraySegment <byte>(System.Text.Encoding.Default.GetBytes("HOLA")); var buffer = new ArraySegment <byte>(new byte[1024]); await clientSocket.SendAsync(message, WebSocketMessageType.Text, true, ct.Token); await clientSocket.ReceiveAsync(buffer, ct.Token); Assert.AreEqual(System.Text.Encoding.UTF8.GetString(buffer.Array).Substring(0, 100), Json.Serialize(BigDataWebSocket.BigDataObject).Substring(0, 100), "Initial chars are equal"); #else var clientSocket = new WebSocket(webSocketUrl); await clientSocket.ConnectAsync(ct.Token); clientSocket.OnMessage += (s, e) => { Assert.AreEqual(Json.Serialize(BigDataWebSocket.BigDataObject), e.Data); }; Assert.AreEqual(WebSocketState.Open, clientSocket.State, "Connection is open"); var buffer = System.Text.Encoding.UTF8.GetBytes("HOLA"); await clientSocket.SendAsync(buffer, Opcode.Text, ct.Token); await Task.Delay(500, ct.Token); #endif }
public async Task TestSendBigDataWebSocket() { var webSocketUrl = WebServerUrl.Replace("http", "ws") + "bigdata"; var wasSet = false; var clientSocket = new WebSocket(webSocketUrl); await clientSocket.ConnectAsync(); clientSocket.OnMessage += (s, e) => { Assert.AreEqual(Json.Serialize(BigDataWebSocket.BigDataObject), e.Data); wasSet = true; }; Assert.AreEqual(WebSocketState.Open, clientSocket.State, "Connection is open"); var buffer = System.Text.Encoding.UTF8.GetBytes("HOLA"); await clientSocket.SendAsync(buffer, Opcode.Text); await Task.Delay(TimeSpan.FromSeconds(1)); Assert.True(wasSet); }