有没有一种方法可以将相同的消息发送到多个客户端,而不会在websocket .net核心中产生循环?我有以下服务器:
这是我当前发送邮件的方式
public async Task SendMessageAsync(WebSocketConnection connection, string message)
{
if (connection.WebSocket.State != WebSocketState.Open) return;
var arr = Encoding.UTF8.GetBytes(message);
var buffer = new ArraySegment<byte>(
array: arr,
offset: 0,
count: arr.Length);
await connection.WebSocket.SendAsync(
buffer: buffer,
messageType: WebSocketMessageType.Text,
endOfMessage: true,
cancellationToken: CancellationToken.None
);
}
这是websocket连接:
public abstract class WebSocketConnection
{
public WebSocketHandler Handler { get; }
public WebSocket WebSocket { get; set; }
public WebSocketConnection(WebSocketHandler handler)
{
Handler = handler;
}
}