谅解`WebSocketServer`中的问题不是一个类

时间:2019-01-26 13:44:00

标签: node.js

我正在实施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

1 个答案:

答案 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');
});