-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (43 loc) · 1.48 KB
/
index.js
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const fs = require('fs');
const http = require('http');
const WebSocket = require('ws');
const { stringify } = require('querystring');
const app=express();
app.use(cors());
app.use(bodyParser.json());
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const port = 8000;
var waiting = null;
let clients = [];
let clickCount = 0;
wss.on('connection', (ws) => {
clients.push(ws);
ws.on('message', (message) => {
if (message.toString() === 'buttonClicked') {
clickCount++;
if (clickCount == 2) {
const ID = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var cur = ID.charAt(Math.random()*25);
cur += ID.charAt(Math.random()*25);
cur += ID.charAt(Math.random()*25);
cur += ID.charAt(Math.random()*25);
console.log(cur);
clients[0].send(cur);
clients[1].send(cur);
clickCount = 0; // Reset the counter after alerting both clients
}
}else{
// console.log(message.toString());
clients[0].send(message.toString());
clients[1].send(message.toString());
}
});
ws.on('close', () => {
clients = clients.filter(client => client !== ws);
});
});
server.listen(port,()=> {console.log("listening");})