Skip to content

Commit febd467

Browse files
author
danil-nizamov
committed
added client folder
1 parent b6a54a1 commit febd467

File tree

7 files changed

+17
-16
lines changed

7 files changed

+17
-16
lines changed

.DS_Store

8 KB
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

server.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,45 +57,45 @@ function serveFile(filePath, res) {
5757
// Handle POST requests
5858
function handlePostRequest(req, res) {
5959
const parsedUrl = url.parse(req.url, true);
60-
60+
6161
if (parsedUrl.pathname === '/message') {
6262
let body = '';
63-
63+
6464
req.on('data', chunk => {
6565
body += chunk.toString();
6666
});
67-
67+
6868
req.on('end', () => {
6969
try {
7070
const data = JSON.parse(body);
7171
const message = data.message;
72-
72+
7373
if (!message) {
7474
res.writeHead(400, { 'Content-Type': 'application/json' });
7575
res.end(JSON.stringify({ error: 'Message is required' }));
7676
return;
7777
}
78-
78+
7979
// Check if WebSocket is available
8080
if (!isWebSocketAvailable) {
8181
res.writeHead(503, { 'Content-Type': 'application/json' });
82-
res.end(JSON.stringify({
83-
error: 'WebSocket functionality not available',
84-
details: 'Install the ws package with: npm install ws'
82+
res.end(JSON.stringify({
83+
error: 'WebSocket functionality not available',
84+
details: 'Install the ws package with: npm install ws'
8585
}));
8686
return;
8787
}
88-
88+
8989
// Broadcast message to all connected WebSocket clients
9090
wsClients.forEach(client => {
9191
if (client.readyState === WebSocket.OPEN) {
9292
client.send(JSON.stringify({ type: 'message', message: message }));
9393
}
9494
});
95-
95+
9696
res.writeHead(200, { 'Content-Type': 'application/json' });
9797
res.end(JSON.stringify({ success: true, clientCount: wsClients.size }));
98-
98+
9999
} catch (error) {
100100
res.writeHead(400, { 'Content-Type': 'application/json' });
101101
res.end(JSON.stringify({ error: 'Invalid JSON' }));
@@ -111,7 +111,7 @@ function handlePostRequest(req, res) {
111111
const server = http.createServer((req, res) => {
112112
const parsedUrl = url.parse(req.url, true);
113113
let pathname = parsedUrl.pathname;
114-
114+
115115
// Handle POST requests
116116
if (req.method === 'POST') {
117117
handlePostRequest(req, res);
@@ -124,10 +124,11 @@ const server = http.createServer((req, res) => {
124124
}
125125

126126
// Remove leading slash and construct file path
127-
const filePath = path.join(__dirname, pathname.substring(1));
127+
const filePath = path.join(__dirname, 'client', pathname.substring(1));
128128

129129
// Security check - prevent directory traversal
130-
if (!filePath.startsWith(__dirname)) {
130+
const clientDir = path.join(__dirname, 'client');
131+
if (!filePath.startsWith(clientDir)) {
131132
res.writeHead(403, { 'Content-Type': 'text/plain' });
132133
res.end('Forbidden');
133134
return;
@@ -153,12 +154,12 @@ if (isWebSocketAvailable) {
153154
wss.on('connection', (ws) => {
154155
console.log('New WebSocket client connected');
155156
wsClients.add(ws);
156-
157+
157158
ws.on('close', () => {
158159
console.log('WebSocket client disconnected');
159160
wsClients.delete(ws);
160161
});
161-
162+
162163
ws.on('error', (error) => {
163164
console.error('WebSocket error:', error);
164165
wsClients.delete(ws);

0 commit comments

Comments
 (0)