启用S​​SL的网站无法连接到本地C#Web套接字服务器

时间:2016-03-15 08:54:41

标签: c# html5 ssl websocket

我正在使用local web socket server implemented in C#。当我连接到没有SSL的托管html页面时它工作正常,但是当启用SSL时,Web套接字连接自动关闭。

在库的readMe.txt文件中,它表示附加一个认证文件。 以下是我创建和附加文件的方法。右键单击Project并执行Sign Manifest并输入密码。然后我将Certification属性设置如下。

WebSocketServer server = new WebSocketServer("wss://0.0.0.0:8431");
string path = "C:\\Users\\Dehan\\Documents\\MySolutionName\\";
server.Certificate = new X509Certificate2(path+"certificateName.pfx", "certificate_pwd", X509KeyStorageFlags.MachineKeySet);
server.Start(socket =>
            {
                socket.OnOpen = () =>
                {                        
                   //inside onopen
                };

                socket.OnError = error =>
                {
                    //inside onerror
                };

                socket.OnClose = () =>
                {
                   //inside onclose
                };

                socket.OnMessage = message =>
                {
                 //inside onmessage
                }

           });

Html page

enter image description here

以下代码显示了html页面的javascript

<script type="text/javascript">
    var start = function () {
        var inc = document.getElementById('incomming');
        var wsImpl = window.WebSocket || window.MozWebSocket;
        var form = document.getElementById('sendForm');
        var input = document.getElementById('sendText');

        inc.innerHTML += "connecting to server ..<br/>";

        // create a new websocket and connect
        window.ws = new wsImpl('wss://127.0.0.1:8431/');

        // when data is comming from the server, this metod is called
        ws.onmessage = function (evt) {
            inc.innerHTML += evt.data + '<br/>';
        };

        // when the connection is established, this method is called
        ws.onopen = function () {
            inc.innerHTML += '.. connection open<br/>';
        };

        // when the connection is closed, this method is called
        ws.onclose = function () {
            inc.innerHTML += '.. connection closed<br/>';
        }

        form.addEventListener('submit', function(e){
            e.preventDefault();
            var val = input.value;
            ws.send(val);
            input.value = "";
        });

    }
    window.onload = start;
</script>

1 个答案:

答案 0 :(得分:0)

server.Certificate = new X509Certificate2(path+"certificateName.pfx", "certificate_pwd", X509KeyStorageFlags.MachineKeySet);

在上面的代码行中,如果您使用的是自签名证书,请确保将其添加到您的计算机受信任的根权限中。