-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
26 lines (19 loc) · 978 Bytes
/
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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){ //istek geldiğinde index.htmlyi açacak.
res.sendfile('index.html');
});
io.on('connection', function(socket){ //tüm socket olaylarında çalışır. bir kullanıcının bağlanması, clientten sunucuya istek yollanması gibi.
console.log('Kullanıcı bağlandı'); //
socket.on('disconnect', function(){ //disconnect olayında çalışan fonksiyon
console.log('kullanıcı ayrıldı');
});
socket.on('sohbetmesaj', function(msg){ //index.htmlden soket üzerinden sohbetmesaj olayı geldiğinde çalışacak.
console.log('Mesaj: ' + msg); //konsolda mesajı yazdır.
io.emit('sohbetmesaj', msg); //sunucudan diğer clientlere sohbetmesaj ve mesajı gönder
});
});
http.listen(2000, function(){ //2000 portundan http dinleme isteği oluşturuluyor.
console.log('2000 portu dinleniyor. ');
});