I'm a beginner of node.js. Recently, I'm trying to use node.js to build a WebSocket server. Here is my code(simplified) and question.
const https = require('https');
const WebSocket = require('ws');
const wss = new WebSocket.Server({port: 3000});
......
wss.on('connection',wss => {
console.log("Connected.");
console.log("wss.clients"); //undefined
});
......
I wanna print all clients in the console, but the console printed "undefined". I searched on the web and connot find a way to solve it.
firstly, according to 'ws' npm package , you should use WebSocketServer
:
const WebSocket = require('ws');
const wss = new WebSocket.WebSocketServer({port: 3000});
secondly, you should use different name for the callback parameter:
wss.on('connection', socket => {
});
finally:
// console.log("wss.clients");
console.log(wss.clients);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.