我在 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 上运行,所以当使用不安全的 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 下使用的服务器证书(不要与 HTTPS 混淆,HTTPS 是 HTTP over TLS)。如果您在 fleck 的网页中看到示例,您就会意识到必须设置证书:
server.Certificate = new X509Certificate2("MyCert.pfx");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句