[英]Connect JS secure websocket to C# websocket server (Fleck)
我在C#(asp.net)中有一個API,我使用fleck運行這個websocket服務器:
SocketService.start();
SocketService.server.Start(socket =>
{
socket.OnOpen = () =>
{
SocketService.Connessione(socket);
};
socket.OnClose = () =>
{
SocketService.Disconnesione(socket);
};
socket.OnMessage = message =>
{
SocketService.Messaggio(message, socket);
};
});
這是SocketService.Start():
public static void start()
{
server = new WebSocketServer($"wss://{GetLocalIPAddress()}:{"4450"}/BNS/");
}
我嘗試使用不安全的ws的簡單HTML / JS頁面,它工作正常。
然后我嘗試在我的主程序中,我需要它在HTTPS上運行所以當使用unsecure ws chrome告訴我使用wss代替。
所以我將我的ws服務器更改為wss但是它什么也沒做,它給了我超時錯誤。
這是JS代碼:
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 { }
};
}
我無法弄清楚如何使它工作。
在此先感謝您的幫助!
您似乎沒有設置要在WS over TLS下使用的服務器證書(不要與HTTP over TLS上的HTTPS混淆)。 如果你在fleck的網頁上看到這個例子,你會發現你必須設置證書:
server.Certificate = new X509Certificate2("MyCert.pfx");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.