使用Fleck库的WebSockets

时间:2014-06-17 16:08:06

标签: c# websocket chat

我尝试使用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?

非常感谢!

1 个答案:

答案 0 :(得分:0)

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

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

更新您的客户端网址