WebSockets using Fleck library

本文关键字:library Fleck using WebSockets | 更新日期: 2023-09-27 18:09:04

我尝试使用websockets和Fleck库创建简单的聊天室。现在我可以向所有用户发送消息。下面是我的代码:

FleckLog.Level = LogLevel.Info;
        var allsockets = new List<IWebSocketConnection>();
        var server = new WebSocketServer("ws://localhost:8181");
        server.Start(socket =>
        {
            socket.OnOpen = () =>
                {   //See socket.ConnectionInfo.* for additional informations
                    Console.WriteLine(String.Empty);
                    Console.WriteLine("[NEW CLIENT CONNECTION]======================");
                    Console.WriteLine("GUID: " + socket.ConnectionInfo.Id);
                    Console.WriteLine("IP: " + socket.ConnectionInfo.ClientIpAddress);
                    Console.WriteLine("Port: " + socket.ConnectionInfo.ClientPort);
                    Console.WriteLine("=============================================");
                    Console.WriteLine(String.Empty);
                    allsockets.Add(socket);
                };
            socket.OnClose = () =>
            {
                    Console.WriteLine(String.Empty);
                    Console.WriteLine("[DISCONNECTED CLIENT]=======================");
                    Console.WriteLine("GUID: " + socket.ConnectionInfo.Id);
                    Console.WriteLine("IP: " + socket.ConnectionInfo.ClientIpAddress);
                    Console.WriteLine("Port: " + socket.ConnectionInfo.ClientPort);
                    Console.WriteLine("=============================================");
                    Console.WriteLine(String.Empty);
                    allsockets.Remove(socket);
                };
            socket.OnMessage = (message) =>
            {
                //TODO: Json.Net Deserialize
                Console.WriteLine("[JSON MESSAGE] " + message);
                allsockets.ToList().ForEach(s => s.Send(message));
            };
        });
客户端代码(java脚本):
    // Websocket Endpoint url
var URL = 'ws://localhost:8181';
var chatClient = null;
function connect () {
    chatClient = new WebSocket(URL);
    chatClient.onmessage = function (event) {
        var messagesArea = document.getElementById("messages");
        var jsonObj = JSON.parse(event.data);
        var message = "<"+ jsonObj.user + "> " + jsonObj.message + "'r'n";
        messagesArea.value = messagesArea.value + message;
        messagesArea.scrollTop = messagesArea.scrollHeight;
    };
}
function disconnect () {
    chatClient.close();
    }
function sendMessage() {
    var user = document.getElementById("userName").value.trim();
    if (user === "")
        alert ("Please enter your name!");
    var inputElement = document.getElementById("messageInput");
    var message = inputElement.value.trim();
    if (message !== "") {
        var jsonObj = {"user" : user, "message" : message};
        chatClient.send(JSON.stringify(jsonObj));
        inputElement.value = "";
    }
    inputElement.focus();
}

如何在此代码中添加安全层WSS?

非常感谢!

WebSockets using Fleck library

您检查过https://github.com/statianzo/Fleck了吗?

您需要有一个证书并使用wss://

更新您的客户端url