-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (23 loc) · 923 Bytes
/
Copy pathindex.js
File metadata and controls
29 lines (23 loc) · 923 Bytes
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
var express = require("express");
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static('public'));
// Маршрут для головної сторінки
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
// Обробка підключень через Socket.IO
io.on('connection', function(socket){
console.log('Користувач підключився до чату');
// Коли сервер отримує повідомлення від клієнта
socket.on('send message', function(msg) {
socket.broadcast.emit('receive message', msg);
});
socket.on('disconnect', function(){
console.log('Користувач відключився');
});
});
http.listen(3000, function() {
console.log('Сервер запущено! Відкрийте http://localhost:3000');
});