简体   繁体   中英

Connect JS secure websocket to C# websocket server (Fleck)

I have an API in C# (asp.net) in which i'm running this websocket server using fleck:

SocketService.start();

            SocketService.server.Start(socket =>
            {
                socket.OnOpen = () =>
                {
                    SocketService.Connessione(socket);
                };
                socket.OnClose = () =>
                {
                    SocketService.Disconnesione(socket);
                };
                socket.OnMessage = message =>
                {
                    SocketService.Messaggio(message, socket);
                };
            });

This is SocketService.Start():

public static void start()
    {
        server = new WebSocketServer($"wss://{GetLocalIPAddress()}:{"4450"}/BNS/");
    }

I have tried with a simple HTML/JS page using unsecure ws and it worked fine.

Then I have tried in my main program which i need it to be run on HTTPS so when using unsecure ws chrome told me to use wss instead.

So i change my ws server to wss but then it does nothing, it gives me timeout error.

This is the JS code:

var start = function () {
        var wsImpl = window.WebSocket || window.MozWebSocket;
        var form = document.getElementById('sendForm');
        var input = document.getElementById('sendText');

        alert("Connessione...");

        // create a new websocket and connect
        window.ws = new wsImpl('@Percorsi.IndirizzoSocket');

        alert("conn");

        // when the connection is established, this method is called
            ws.onopen = function () {
                alert("Connessione aperta");
                 var openJson = {
                    "Id": "@Model.accountCorrente.Id",
                    "type": "Identificazione"
                 };

                alert("send");
                ws.send(stringify(openJson));
            };
            // when the connection is closed, this method is called
            ws.onclose = function () {
                alert("Connessione chiusa");
            }
            // when data is comming from the server, this metod is called
            ws.onmessage = function (val) {
                if (confirm("Hai ricevuto un nuovo messaggio!\nPremi ok per visualizzarlo.")) {
                    window.location("/Annunci/Chat/" + val);
                } else { }
            };

    }

I can't figured out how to make it works.

Thanks in advance for your help!

It seems like you are not setting the server certificate to be used under WS over TLS (not to be confused with HTTPS which is HTTP over TLS). If you see the example in fleck's webpage , you will realize that you have to set the Certificate:

server.Certificate = new X509Certificate2("MyCert.pfx"); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM