I'm using a local web socket server implemented in C# . Its working fine when I connect to hosted html page without SSL, but when there is SSL enabled, Web socket connection closed automatically.
In the readMe.txt file of the library, it says to attach a certification file. Here is how I create and attach the file. Right click the Project and do Sign the Manifest and gave a password. Then I have set the Certification
property as follows.
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
}
});
following code shows the javascript of the html page
<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>
server.Certificate = new X509Certificate2(path+"certificateName.pfx", "certificate_pwd", X509KeyStorageFlags.MachineKeySet);
在上面的代码行中,如果您使用的是自签名证书,请确保已将其添加到计算机的受信任的根颁发机构中。
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.