// websocket.js
const ws = require('nodejs-websocket')
let clientCount = 0;
let server = ws.createServer(connection => {
clientCount++;
connection.nickname = 'user' + clientCount;
boardCast(connection.nickname + '进入房间');
connection.on('text', function (result) {
console.log('接收消息:', result)
boardCast(connection.nickname + ':' + result + '!!!')
})
connection.on('connect', function (code) {
console.log('接收消息:', code)
})
connection.on('close', function (code) {
boardCast(connection.nickname + '离开了房间');
console.log('接收消息:', code)
})
connection.on('error', function (code) {
console.log('接收消息:', code)
})
}).listen(8001)
function boardCast(mess) {
server.connections.map((con) => {
con.sendText(mess)
});
}