This repository was archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttptowebsocket.js
121 lines (96 loc) · 3.03 KB
/
httptowebsocket.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const express = require('express');
const http = require('http');
const socketio = require('socket.io');
const app = express();
const server = http.createServer(app);
const wss = socketio(server, {
cors: {
origin: '*',
}
});
var currentState = {
pomodoro: {
state: -1, // 0 = stopped, 1 = running, 2 = results, 3 = paused
mode: 0, // -1 = none, 0 = study, 1 = game
time: 25, // in minutes
},
}
function viewer(req, res, buf, encoding) {
console.log(buf.toString());
}
// Middleware to parse JSON in POST requests
app.use(express.json({ verify: viewer }));
// Handle HTTP POST requests
app.post('/openbci', (req, res) => {
console.log('POST /openbci');
//console.log(req.body);
// Broadcast the received message to all WebSocket clients
console.log(currentState.pomodoro.state);
if (currentState.pomodoro.state == 0) {
wss.emit('rt-openbci', req.body["Focus Score"]);
} else {
wss.emit('openbci', req.body);
}
res.status(200).json({ success: true });
});
app.post('/mouse', (req, res) => {
console.log('POST /mouse');
const message = req.body;
console.log(currentState.pomodoro.state);
// Broadcast the received message to all WebSocket clients
wss.emit('mouse', message);
res.status(200).json({ success: true });
})
app.post('/gaze_mouse', (req, res) => {
console.log('POST /gaze_mouse');
const message = req.body;
console.log(currentState.pomodoro.state);
// Broadcast the received message to all WebSocket clients
wss.emit('gaze_mouse', message);
res.status(200).json({ success: true });
});
app.post('/activity', (req, res) => {
console.log('POST /activity');
//console.dir(req.body);
//console.log(currentState.pomodoro.state);
if (currentState.pomodoro.state == 0) {
wss.emit('rt-activity', req.body);
} else {
req.body = req.body[0].replaceAll("][", '],[')
req.body = JSON.parse("[" + req.body + "]")
wss.emit('activity', req.body);
}
// Broadcast the received message to all WebSocket clients
res.status(200).json({ success: true });
})
app.post('/gaze', (req, res) => {
const message = req.body;
console.log(currentState.pomodoro.state);
// Broadcast the received message to all WebSocket clients
wss.emit('gaze', message);
res.status(200).json({ success: true });
});
app.get('/status', function (request, response) {
//console.log('GET /status')
response.writeHead(200, { 'Content-Type': 'application/json' })
response.end(JSON.stringify(currentState))
})
// WebSocket connection handling
wss.on('connection', (ws) => {
console.log('WebSocket client connected');
ws.on('message', (message) => {
if (message.cmd == "init")
currentState.pomodoro.state = 0;
else if (message.cmd == "stop")
currentState.pomodoro.state = 1;
else if (message.cmd == "reset")
currentState.pomodoro.state = -1;
console.log(`Received message: ${message}`);
});
ws.on('close', () => {
console.log('WebSocket client disconnected');
});
});
server.listen(8327, () => {
console.log('Server is running on port 8327');
});