gpt4 book ai didi

What is the best approach to holding multiple WebSocket clients in memory in ASP.NET Core(在ASP.NET Core的内存中保存多个WebSocket客户端的最佳方法是什么)

转载 作者:bug小助手 更新时间:2023-10-24 21:59:39 27 4
gpt4 key购买 nike



I am trying to implement a way to hold multiple WebSocket client connections at a time. The reason for this is because I would like to start and stop these connections through API endpoints as well as send messages and receive messages.

我正在尝试实现一种同时保持多个WebSocket客户端连接的方法。这是因为我希望通过API端点启动和停止这些连接,以及发送和接收消息。


For context, I am wanting to connect to multiple Remote Consoles (RCON) to receive and send messages to them. Sometimes the server hosting the RCON may restart, and being able to reconnect is vital.

对于上下文,我想连接到多个远程控制台(RCON)来接收和发送消息到它们。有时,托管RCON的服务器可能会重新启动,能够重新连接至关重要。


My current solution allows me to start, stop and send messages, however the EventHandlers do not fire (except for the opening of the WebSocket).

我当前的解决方案允许我启动、停止和发送消息,但是EventHandler不会触发(除了打开WebSocket之外)。


using System.Collections.Concurrent;
using WebSocketSharp;

namespace Rcon;

public interface IWebSocketManager
{
void AddWebSocketConnection(string key, WebSocket webSocket);

void RemoveWebSocketConnection(string key);

void Send(string key, string message);

WebSocket? GetWebSocketConnection(string key);
}

public class WebSocketManager : IWebSocketManager
{
private readonly ConcurrentDictionary<string, WebSocket> _connections = new();

public void AddWebSocketConnection(string key, WebSocket webSocket)
{
_connections.TryAdd(key, webSocket);
}

public void RemoveWebSocketConnection(string key)
{
_connections.TryRemove(key, out _);
}

public void Send(string key, string message)
{
if (_connections.TryGetValue(key, out WebSocket? webSocket))
webSocket.Send(message);
}

public WebSocket? GetWebSocketConnection(string key)
{
_connections.TryGetValue(key, out WebSocket? connection);

return connection;
}
}

Registering the dependency:

注册依赖项:


services.AddSingleton<IWebSocketManager, WebSocketManager>()

I am adding it to the concurrent dictionary with the following method, using the IWebSocketManager dependency:

我使用以下方法,使用IWebSocketManager依赖项将其添加到并发字典中:


public void AddConnection(ConnectionDetails connectionDetails)
{
WebSocket ws = new(connectionDetails.ConnectionString);

ws.OnOpen += (_, _) =>
{
// Fires
Console.WriteLine(
$"Connected to WebSocket. Url: {connectionDetails.Ip}:{connectionDetails.Port}");
};

ws.OnClose += (_, _) =>
{
// DOES NOT FIRE
try
{
Console.WriteLine(
$"Disconnected from WebSocket. Url: {connectionDetails.Ip}:{connectionDetails.Port}");

_webSocketManager.RemoveWebSocketConnection(connectionDetails.CreateWSMKey());
}
catch (Exception)
{
// TODO
}
};

ws.OnMessage += (_, e) =>
{
// DOES NOT FIRE
Console.WriteLine("Received message from WebSocket");
};

ws.Connect();

_webSocketManager.AddWebSocketConnection(connectionDetails.CreateWSMKey(), ws);
}

I would like to make calling Send() on WebSockets incredibly easy as exampled below:

我想让在WebSocket上调用Send()变得非常简单,如下所示:


    private readonly IWebSocketManager _webSocketManager;

public SocketController(IWebSocketManager webSocketManager)
{
_webSocketManager = webSocketManager;
}

[HttpPost]
[Route("sendsocketmessage/{message}")]
public ActionResult SendSocketMessage(string message)
{
ConnectionDetails connectionDetails = new("localhost", 3000, "password");

var socket = _webSocketManager.GetWebSocketConnection(connectionDetails.CreateWSMKey());

socket?.Send(message);

return Ok();
}

The above does work with my current implementation. But none of this matters if OnMessage event doesn't trigger.

上面的方法确实适用于我当前的实现。但是,如果OnMessage事件不触发,这些都无关紧要。


The approach I am taking does not feel right, but it almost works... How can I improve my solution to this problem. Is RabbitMQ a good option, I think it will remove some of the functionality that I want however.

我采取的方法感觉不正确,但几乎奏效了……我怎样才能改进我对这个问题的解决方案呢?RabbitMQ是一个好的选择吗,我想它会去掉我想要的一些功能。


更多回答

Your events have to be registered using an instance which is the property "_connections[index]". So your method AddConnection needs a parameter for _connection[i].

您的事件必须使用属性为“_Connections[index]”的实例进行注册。因此,您的方法AddConnection需要_Connection[i]的参数。

优秀答案推荐
更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com