我拍了一个我朋友制作的游戏,希望通过WebRTC和websockets在对等体之间发送按键数据,让它可以跨浏览器播放。但是,我在控制台中收到此错误:
WebSocket连接到' ws:// localhost:3000 /'失败:在收到握手响应之前关闭连接
我的服务器文件包含以下几行:
'use strict';
const express = require('express');
const SocketServer = require('ws').Server;
const path = require('path');
const PORT = process.env.PORT || 3000;
const INDEX = path.join(__dirname, 'index.html');
const server = express();
server.use(express.static(path.join(__dirname, 'lib')));
server.use('/assets', express.static(path.join(__dirname, 'assets')));
server.listen(PORT, () => console.log(`Listening on ${ PORT }`));
const wss = new SocketServer({ server });
var users = {};
let usernames = [];
wss.on('connection', function(connection) {
connection.on('message', function(message) {
var data;
try {
data = JSON.parse(message);
} catch (e) {
console.log("Invalid JSON");
data = {};
}
switch (data.type) {
case "login":
console.log("User logged", data.name);
if(users[data.name]) {
sendTo(connection, {
type: "login",
success: false
});
} else {
users[data.name] = connection;
connection.name = data.name;
usernames.push(data.name);
sendTo(connection, {
type: "login",
success: true,
users: usernames
});
}
break;
case "offer":
console.log("Sending offer to: ", data.name);
var conn = users[data.name];
if(conn != null) {
connection.otherName = data.name;
sendTo(conn, {
type: "offer",
offer: data.offer,
name: connection.name
});
}
break;
case "answer":
console.log("Sending answer to: ", data.name);
var conn = users[data.name];
if(conn != null) {
connection.otherName = data.name;
sendTo(conn, {
type: "answer",
answer: data.answer
});
}
break;
case "candidate":
console.log("Sending candidate to:",data.name);
var conn = users[data.name];
if(conn != null) {
sendTo(conn, {
type: "candidate",
candidate: data.candidate
});
}
break;
case "leave":
console.log("Disconnecting from", data.name);
var conn = users[data.name];
conn.otherName = null;
if(conn != null) {
sendTo(conn, {
type: "leave"
});
}
break;
default:
sendTo(connection, {
type: "error",
message: "Command not found: " + data.type
});
break;
}
});
连接的客户端看起来如下:
const Game = require("./game");
const GameView = require("./game_view");
var HOST = location.origin.replace(/^http/, 'ws');
console.log('host: ', HOST);
console.log(process.env.PORT);
document.addEventListener("DOMContentLoaded", function() {
const connection = new WebSocket(HOST);
.....
这是错误发生的点,这是我得到的捕获错误:
bubbles
:
false
cancelBubble
:
false
cancelable
:
false
composed
:
false
currentTarget
:
WebSocket
defaultPrevented
:
false
eventPhase
:
0
isTrusted
:
true
path
:
Array(0)
returnValue
:
true
srcElement
:
WebSocket
target
:
WebSocket
timeStamp
:
213.01500000000001
type
:
"error"
__proto__
:
Event
我对服务器端编程并不太熟悉,并试图理解。我试着查找这个问题,但似乎各种不同的东西都可能导致这个问题。如果您想查看存储库,可以自己查看并尝试使用(使用webpack):SlidingWarfare Repo
答案 0 :(得分:6)
混乱从这里开始:
const server = express();
express
函数并不真正返回服务器,它返回一个应用程序。通常,用于此的变量是app
,但这当然不过是约定(即不是要求)。
但是,将应用程序传递给WS服务器时会出现问题:
const wss = new SocketServer({ server });
那是因为SocketServer
需要一个HTTP服务器实例,server
不是。
这是一个修复,没有重命名变量:
let httpServer = server.listen(PORT, () => console.log(`Listening on ${ PORT }`));
...
const wss = new SocketServer({ server : httpServer });
(因为当你在Express实例上调用.listen()
时,它将返回一个HTTP服务器实例)
使用变量命名约定:
const app = express();
app.use(express.static(path.join(__dirname, 'lib')));
app.use('/assets', express.static(path.join(__dirname, 'assets')));
let server = app.listen(PORT, () => console.log(`Listening on ${ PORT }`));
const wss = new SocketServer({ server });