forked from jrainlau/draw-something
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathws-server.js
More file actions
33 lines (28 loc) · 885 Bytes
/
ws-server.js
File metadata and controls
33 lines (28 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
'use strict'
const WebSocketServer = require('ws').Server
, wss = new WebSocketServer({port: 8090})
let wordArr = ['Monkey', 'Dog', 'Bear', 'Flower', 'Girl']
wss.on('connection', function(ws) {
console.log('connected.')
let keyWord = ((arr) => {
let num = Math.floor(Math.random()*arr.length)
return arr[num]
})(wordArr)
ws.on('message', function(message) {
console.log('received: %s', message)
if (message == keyWord) {
console.log('correct')
wss.clients.forEach((client) => {
client.send('答对了!!')
})
} else {
console.log('wrong')
wss.clients.forEach((client) => {
client.send(message)
})
}
})
wss.clients.forEach((client) => {
client.send('keyword:' + keyWord)
})
})