我正在实施websockets并在我的项目中也安装了ws软件包,但是当我初始化const WSS= new WebSocketServer({port: 3232})
时,typeError显示为WebSocketServer is not a constructor
。
const WebSocketServer = require('ws').server;
const WSS = new WebSocketServer({port: 3232});
WSS.on('connection',(ws)=>{
console.log('we are connected');
});
显示 typeError webSocketServer is not a constructor
答案 0 :(得分:0)
它是.Server
,而不是.server
。
这是一个例子:
const WebSocket = require('ws');
const wss = new WebSocket.Server({
port: 8080
})
或保留您的格式:
const WebSocketServer = require('ws').Server;
const WSS = new WebSocketServer({port: 3232});
WSS.on('connection',(ws)=>{
console.log('we are connected');
});