-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
167 lines (122 loc) · 4.61 KB
/
server.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const express = require('express');
// const request = require('request'); // using 'request' npm package
const https = require('https');
const fs = require('fs');
const WebSocket = require('ws');
// Create two Express app instances
const appHome = express();
const appEmotion = express();
// const app = express();
const portEmotion = 4443;
const path = require('path');
appEmotion.use('/emotion',express.static('public/emotion'));
appEmotion.use('/libs', express.static(path.join(__dirname, 'libs')));
console.log(path.join(__dirname, 'libs'));
appEmotion.use('/weights', express.static(path.join(__dirname, 'weights')));
appEmotion.use('/uuid', express.static(path.join(__dirname, 'node_modules/uuid/dist/esm-browser')));
// Serve static files for the homepage
appHome.use(express.static(path.join(__dirname, 'public')));
appHome.use('/.well-known', express.static(path.join(__dirname, 'public', '.well-known')));
// // Serve static files for the emotion page
// appEmotion.use(express.static(path.join(__dirname, 'public/emotion')));
// SSL certificate details
const privateKey = fs.readFileSync('private/private.key', 'utf8');
const certificate = fs.readFileSync('private/certificate.crt', 'utf8');
const ca = fs.readFileSync('private/ca_bundle.crt', 'utf8');
const credentials = {
key: privateKey,
cert: certificate,
ca: ca
};
// Listen on port 80 for the homepage
appHome.listen(80, () => {
console.log('Homepage is running on http://localhost:80');
});
// Listen on port 4443 for the emotion page using HTTPS
const server=https.createServer(credentials, appEmotion).listen(portEmotion, '0.0.0.0', () => {
console.log(`Server running at https://localhost:${portEmotion}/`);
});
const wss = new WebSocket.Server({ server });
date=new Date().toDateString();
const filePath = path.join(__dirname, 'data', 'emotionData'+date+'.txt');
console.log(filePath);
const buffer = [];
const MAX_BUFFER_SIZE = 1000;
const receiver='Hidalgo_WhoAreYou';
// Object to store client WebSocket connections using clientId as key
const clients = {};
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('message', (message) => {
try {
const payload = JSON.parse(message);
const { clientId, emotionData,landmarks,tmpA,tmpB } = payload;
const dataToSend={
clientId,
emotionData
}
if (clientId) {
clients[clientId] = ws;
}
// console.log('Client ID:', clientId);
// console.log('Received:', emotionData);
payload.timestamp = new Date().toISOString();
if (receiver in clients) {
// sendMessageToClient(receiver, "you are Paul?");
// console.log('found receiver');
if (emotionData && Object.keys(emotionData).length > 0) {
sendMessageToClient(receiver, JSON.stringify(dataToSend));
}
}
buffer.push(JSON.stringify(payload));
if (buffer.length >= MAX_BUFFER_SIZE) {
const dataToWrite = buffer.join('\n') + '\n';
buffer.length = 0; // clear the buffer
fs.appendFile(filePath, dataToWrite, (err) => {
if (err) {
console.log( err);
}
console.log('Data stored');
});
}
} catch (error) {
console.error("Error parsing message:", error);
}
});
ws.on('error', (error) => {
console.error("WebSocket Client Error:", error);
});
ws.send('Welcome to the server!');
});
wss.onerror = function(error) {
console.error("WebSocket Error:", error);
};
// Function to send a message to a specific client using its clientId
function sendMessageToClient(clientId, message) {
const clientWs = clients[clientId];
if (clientWs && clientWs.readyState === WebSocket.OPEN) {
clientWs.send(message);
} else {
console.error("Client not connected or WebSocket not in OPEN state");
}
}
function flushBuffer() {
if (buffer.length > 0) {
const dataToWrite = buffer.join('\n') + '\n';
buffer.length = 0; // clear the buffer
fs.appendFileSync(filePath, dataToWrite); // Use synchronous method to ensure data gets written before exit
console.log('Last bit of data stored');
}
}
// Listen to the exit event
process.on('exit', flushBuffer);
// Optionally, you can also handle other signals like SIGINT (Ctrl+C) or uncaught exceptions
process.on('SIGINT', function() {
flushBuffer();
process.exit(0); // this is required to actually terminate the process after writing the buffer
});
process.on('uncaughtException', function(err) {
console.error('Caught exception:', err);
flushBuffer();
process.exit(1); // this is required to actually terminate the process after writing the buffer
});