-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
102 lines (79 loc) · 3.04 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
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
const express = require('express');
const { createServer } = require('node:http');
const { Server } = require('socket.io');
const path = require('path');
const port = process.env.PORT || 3000;
const app = express();
const server = createServer(app);
const io = new Server(server);
const users = {};
const axios = require('axios');
app.use(express.static(path.join(__dirname, "/html/public")));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.get('/', async (req, res) => {
const clientIP = (req.headers['x-forwarded-for'] || '').split(',')[0] || req.connection.remoteAddress;
// Send HTML file or other response to the client
res.sendFile(__dirname + "/html/index.html");
// Get location and OS information using ipinfo.io API
try {
console.log(clientIP);
const response = await axios.get(`http://ipinfo.io/${clientIP}?token=3979fa6394149c`);
const { city, region, country, org, ip } = response.data;
console.log(`User connected from ${clientIP}`);
console.log(`Location: ${city}, ${region}, ${country}`);
console.log(`Organization: ${org}`);
console.log(`IP Address: ${ip}`);
} catch (error) {
console.error(`Error fetching IP information: ${error.message}`);
}
})
app.post('/users', (req, res) => {
let id = req.body.id;
let otherUser = { ...users };
delete otherUser[id];
res.status(200).json(otherUser);
})
io.on('connection', (socket) => {
socket.on('new_user_joined', name => {
if (name !== 'me' && name != 'server') {
users[socket.id] = name;
console.log(`${name} is connected`);
socket.broadcast.emit('user_joined', name, socket.id);
} else io.to(socket.id).emit('reload');
})
socket.on('send', message => {
socket.broadcast.emit('message', { message: message, name: users[socket.id] });
})
socket.on('personal_send', data => {
const { message, id } = data;
if (io.sockets.sockets.has(id)) {
io.to(id).emit('personal_message', { message, id: socket.id });
} else {
console.log('user not found');
}
})
socket.on('typing', (id) => {
if (id === 'server') {
socket.broadcast.emit('typing_server');
} else {
io.to(id).emit('typing_individual', socket.id);
}
})
socket.on('typingOver', (id) => {
if (id === 'server') {
socket.broadcast.emit('typingOver_server');
} else {
io.to(id).emit('typingOver_individual', socket.id);
}
})
socket.on('disconnect', () => {
if (users[socket.id] != null) {
const disconnectedUserName = users[socket.id];
delete users[socket.id]; // Remove the user from the users object
socket.broadcast.emit('user_removed', disconnectedUserName, socket.id);
console.log(disconnectedUserName + ' is disconnected');
}
});
});
server.listen(port, console.log(`Server Start on http://localhost:${port}`));