今天,我们有一个完全基于桌面的基于事件的应用程序。我们想转到Web并使用Fleck使用Web套接字来驱动事件。我无法弄清楚如何从服务器到客户端异步地发送消息。以下是我要实现的示例

public class Program
{
    static readonly WebSocketServer _webSocketServer = new WebSocketServer("ws://127.0.0.1:8181");
    static readonly List<IWebSocketConnection> SocketClients = new List<IWebSocketConnection>();

    static void Main(string[] args)
    {
        _webSocketServer.Start(socket =>
        {
            socket.OnOpen = () =>
            {
                SocketClients.Add(socket);
                OnOpening(socket.ConnectionInfo.Id);
            };

            socket.OnClose = () =>
            {
                OnClosing(socket.ConnectionInfo.Id);
                SocketClients.Remove(socket);
            };

            socket.OnMessage = message => OnMessage(message, socket.ConnectionInfo.Id);
        });
        Console.ReadLine();
    }

    private static void OnMessage(string message, Guid id)
    {
        Console.WriteLine("<i> Recived message " + message + " from Client Id : " + id + " </i>");
    }


    private static void OnClosing(Guid id)
    {
        Console.WriteLine("<i> Client Left With Id : " + id + "</i>");
    }

    private static void OnOpening(Guid id)
    {
        //Do Something

    }

    // CALL THIS METHOD FROM MY APPLICATION AND SEND THE MESSAGE TO CLIENT BASED ON THE ID
    private static void SendMessage(string message, Guid id)
    {
        // HOW TO DO SOMETHING LIKE BELOW SEND MESSAGE ASYNC
        //_webSocketServer.Start(socket =>
        //{
        //    var clientToSend = SocketClients.Find(client => client.ConnectionInfo.Id == id);
        //    socket.Send(message);
        //});
    }
}

最佳答案

您在他们的GitHub中有一个类似的示例:https://github.com/statianzo/Fleck/blob/master/src/Samples/ConsoleApp/Server.cs

我刚刚添加了SendToSocketById方法>

class Server
{
    static void Main()
    {
        FleckLog.Level = LogLevel.Debug;
        var allSockets = new List<IWebSocketConnection>();
        var server = new WebSocketServer("ws://0.0.0.0:8181");
        server.Start(socket =>
            {
                socket.OnOpen = () =>
                    {
                        Console.WriteLine("Open!");
                        allSockets.Add(socket);
                    };
                socket.OnClose = () =>
                    {
                        Console.WriteLine("Close!");
                        allSockets.Remove(socket);
                    };
                socket.OnMessage = message =>
                    {
                        Console.WriteLine(message);
                        allSockets.ToList().ForEach(s => s.Send("Echo: " + message));
                    };
            });


        var input = Console.ReadLine();
        while (input != "exit")
        {
            foreach (var socket in allSockets.ToList())
            {
                socket.Send(input);
            }
            input = Console.ReadLine();
        }

    }

    static void SendToSocketById(String input, Guid id)
    {
         var socket = allSockets.Find(client => client.ConnectionInfo.Id == id);
         socket.Send(input);
    }
}

10-02 22:02