gpt4 book ai didi

javascript - WebSocket 连接已建立,onopen 从未运行

转载 作者:行者123 更新时间:2023-12-03 03:28:24 27 4
gpt4 key购买 nike

我正在尝试学习 WebSockets,并且我已经在 Node 中创建了一个 websocket 服务器,现在正在研究浏览器实现。我已经使用名为 Smart WebSocket Client 的 chrome 扩展测试了服务器是否正常工作并按照我想要的方式响应。

当您按下按钮并连接丢失时,浏览器中的控制台会显示按钮已按下! (1000) 当我结束 Node 进程但从未说过连接已建立!

编辑:客户端代码在使用 HTTPS 保护的站点上运行,并提供 HSTS header ,而服务器代码(当前,但不会继续)在本地主机上正常运行HTTP,如果有任何问题的话。

服务器代码:

const websock = require('./node_modules/ws');
const HashMap = require('./node_modules/hashmap');
const jsonparse = require('./node_modules/jsonparse');
const randomstring = require('./node_modules/randomstring');

class Session {
constructor(server) {
this.server = server;
this.clients = [];
}
}

var connections = new HashMap();
const json = new jsonparse();

const wss = new websock.Server({ port: 36245 });

process.on('SIGINT',function () {
console.log("Recieved SIGINT, stopping gracefully...");
wss.clients.forEach(function (ws) {
console.log("-Ended connection with "+ws.upgradeReq.socket.remoteAddress+" (1001)");
ws.closeReasonCode = 1001;
ws.close();
});
process.exit(1);
});

wss.on('connection',function connection(ws,conn) {
console.log("+Recieved connection from "+ws._socket.remoteAddress);
ws.upgradeReq = conn;
ws.hasHandshook = false;
ws.onmessage = function message(msg) {
var message;
try {
message = JSON.parse(msg.data);
} catch (ex) {
ws.send("{\"e\":\"Invalid json.\"}");
return;
}
if (!ws.hasHandshook) {
ws.hasHandshook = true;
if (message.type === "client") {
//ensure code was provided and has a room
if (typeof message.code === 'undefined' || !connections.has(message.code)) {
ws.send("{\"e\":\"Invalid game code.\"}");
ws.closeReasonCode = 4001;
ws.closeDescription = "Invalid game code.";
console.log("-Ended connection with "+ws._socket.remoteAddress+ " (4001)");
ws.close();
}
if (typeof message.name === 'undefined') {
//TODO error out, no player name provided
}
//attach client to game session
ws.clientType = "client";
ws.gameCode = message.code;
ws.playerName =
connections.get(message.code).clients.add(ws);
ws.send("{\"joingame\":\"true\"}");
} else {
ws.send("{\"e\":\"Invalid type provided on handshake message.\"}");
ws.closeReasonCode = 4000;
ws.closeDescription = "Invalid type provided on handshake message.";
console.log("-Ended connection with "+ws._socket.remoteAddress+" (4000)");
ws.close();
}
}
};
ws.onclose = function close() {
console.log("-Ended connection with "+ws.upgradeReq.socket.remoteAddress+" (Client Closed)");
}
});

客户端代码,按下页面上的按钮即可成功运行:

function DoJoinGame () {
console.log("Button pressed!");
gameCode = document.getElementById('base-gameCode').value.toUpperCase();
playerName = document.getElementById('base-playerName').value;
var ws = new WebSocket("ws://localhost:36245");
ws.onopen = function (event) {
console.log("Connection Established!");
ws.send("{\"type\":\"client\",\"code\":\""+gameCode+"\",\"name\":\""+playerName+"\"");
};
ws.onmessage = function (msg) {
let message = JSON.parse(msg.data);
if (message.joingame) { //if this is a "client added to session" message, set display: none; on the codeEntry div
document.getElementById('codeEntry').style.display = "none";
}
//TODO handle message
};
ws.onclose = function (evt) {
console.log("Connection lost! ("+evt.code+":"+evt.reason+")");
};
}

感谢您的帮助!

最佳答案

问题已解决。我试图从安全源和 chrome & co 连接到不安全的 websocket 服务器。不是粉丝。

关于javascript - WebSocket 连接已建立,onopen 从未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46207363/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com