-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
73 lines (58 loc) · 2 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
const http = require('http');
const fs = require('fs');
const os = require('os');
const socketIo = require('socket.io');
const express = require('express');
const ioClient = require('socket.io-client');
const app = express();
const server = http.createServer(app);
const io = socketIo(server);
let counter = 0;
let visits = 0;
// Serve the index.html file
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html'); // Fixed path concatenation
});
// WebSocket connection handling
io.on('connection', (socket) => {
const clientIp = socket.handshake.address;
console.log(`Client ip: ${clientIp}`);
visits += 1;
// Make the visits visible
io.emit('update', visits);
socket.emit('update', visits);
// Log the user
console.log('A user has connected');
console.log(`User platform: ${os.platform()}`);
// Send current counter value to newly connected client
socket.emit('counterUpdate', counter);
// Listen for increment events
socket.on('increment', () => {
counter++;
io.emit('counterUpdate', counter); // Broadcast to all connected clients
});
socket.on('disconnect', () => {
console.log('User disconnected');
});
});
// Use the PORT environment variable or default to 3000
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Connect to your Railway-hosted app
const railwayAppUrl = 'https://countercounter-production.up.railway.app';
const socketClient = ioClient(railwayAppUrl);
socketClient.on('connect', () => {
console.log('Connected to Railway app');
});
socketClient.on('userAction', (message) => {
console.log('User action:', message);
// You can add additional logic here, like writing to a file
fs.appendFile('user_actions.log', message + '\n', (err) => {
if (err) console.error('Error writing to log file:', err);
});
});
socketClient.on('disconnect', () => {
console.log('Disconnected from Railway app');
});