-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
41 lines (29 loc) · 981 Bytes
/
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
const express= require('express');
const app=express();
const server=require('http').createServer(app);
const io=require('socket.io').listen(server);
users=[];
connections=[];
app.get('/',(req,res)=>{
res.sendFile(__dirname+'/index.html');
});
// app.listen(3000,(req,res)=>{
// console.log("Server on ort 3000 is active!!");
// });
server.listen(process.env.port||3000,()=>{
console.log("Server on port 3000 is active");
});
io.sockets.on('connection',(socket)=>{
connections.push(socket);
console.log('connected : %s sockets connected',connections.length);
// disconnection
socket.on('disconnect',()=>{
connections.splice(connections.indexOf(socket),1);
console.log("Disconnected: %s sockets connected",connections.length);
});
//send message
socket.on('send message',(data)=>{
console.log(data);
io.sockets.emit('new message',{msg:data});
})
});