C
C#•8mo ago
YaMang -w-

asp.net core send ping websocket closed

[SwaggerIgnore]
[Route("/ws")]
public async Task Open()
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
var buffer = new byte[1024 * 4];
var receiveResult = await webSocket.ReceiveAsync(
new ArraySegment<byte>(buffer), CancellationToken.None);

if (receiveResult.Count == 0)
{
return;
}

var receivedMessage = Encoding.UTF8.GetString(buffer, 0, receiveResult.Count);

char[] delimiters = new char[] { ' ', ',', '.' };
string[] words = receivedMessage.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
if(words.Length == 0)
{
Console.WriteLine($"{receivedMessage} | words 0");
return;
}
Console.WriteLine($"{receivedMessage}");
foreach (var item in words)
{
Debug.WriteLine(item);
}

string key = words[1];
if (!GlobalCache._websocket.ContainsKey(key))
{
GlobalCache._websocket.Add(key, webSocket);
Console.WriteLine($"{key} Websocket Add.");
}

await Echo(webSocket, key);
}
else
{
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
}
}

private async Task Echo(WebSocket webSocket, string key)
{

try
{
var buffer = new byte[1024 * 4];
WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);

while (!result.CloseStatus.HasValue)
{
await SendMessageAsync(webSocket, key, "ping");
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "closing websocket", CancellationToken.None);
//await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error");
}

}

private async Task SendMessageAsync(WebSocket webSocket, string key, string message)
{
var messageBuffer = Encoding.UTF8.GetBytes(message);
var segment = new ArraySegment<byte>(messageBuffer);

await webSocket.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None);
}
[SwaggerIgnore]
[Route("/ws")]
public async Task Open()
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{
using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
var buffer = new byte[1024 * 4];
var receiveResult = await webSocket.ReceiveAsync(
new ArraySegment<byte>(buffer), CancellationToken.None);

if (receiveResult.Count == 0)
{
return;
}

var receivedMessage = Encoding.UTF8.GetString(buffer, 0, receiveResult.Count);

char[] delimiters = new char[] { ' ', ',', '.' };
string[] words = receivedMessage.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
if(words.Length == 0)
{
Console.WriteLine($"{receivedMessage} | words 0");
return;
}
Console.WriteLine($"{receivedMessage}");
foreach (var item in words)
{
Debug.WriteLine(item);
}

string key = words[1];
if (!GlobalCache._websocket.ContainsKey(key))
{
GlobalCache._websocket.Add(key, webSocket);
Console.WriteLine($"{key} Websocket Add.");
}

await Echo(webSocket, key);
}
else
{
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
}
}

private async Task Echo(WebSocket webSocket, string key)
{

try
{
var buffer = new byte[1024 * 4];
WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);

while (!result.CloseStatus.HasValue)
{
await SendMessageAsync(webSocket, key, "ping");
await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
}
await webSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "closing websocket", CancellationToken.None);
//await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error");
}

}

private async Task SendMessageAsync(WebSocket webSocket, string key, string message)
{
var messageBuffer = Encoding.UTF8.GetBytes(message);
var segment = new ArraySegment<byte>(messageBuffer);

await webSocket.SendAsync(segment, WebSocketMessageType.Text, true, CancellationToken.None);
}
send ping websocket closed help me 😦
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server