服务端:
const ws = require('nodejs-websocket')
const arr = {}
const server = ws.createServer((socket) => {
socket.on('text',(str) => {
var data = JSON.parse(str)
console.log(data)
if(arr[data.username]){
for(const item in arr){
arr[item].sendText(JSON.stringify({
username: data.username,
text: data.message
}))
}
}
else{
arr[data.username] = socket
}
})
}).listen(4000)
网页端:
第一个用户
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/cs