const wss = new WebSocketServerport3000; ^TypeError WebSocketServer is not a constructor at filehomeblackCatcodestestappjs313 at ModuleJobrun nodeinternalmodulesesmmodule_job19425Node
在最新版本的Node.js中,创建WebSocket服务器需要使用ws模块,并且通过createServer方法创建服务器。以下是修正后的代码:
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 3000 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('Hello, Client!');
});
请确保您已经安装了ws模块。可以使用以下命令进行安装:
npm install ws
希望能帮到您!
原文地址: http://www.cveoy.top/t/topic/h04X 著作权归作者所有。请勿转载和采集!