-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
70 lines (67 loc) · 1.96 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
/**
* Created by rishabh gupta on 14-07-2017.
*/
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var socket = require('socket.io');
var io = socket(server);
var chat = [];
var users = [];
var port =process.env.PORT || 8080;
app.use('/', express.static('public_static'));
//io means root and hey here referes to person who is sending you the message only
io.on('connection', function (hey) {
console.log('connection is on');
/*hey.on('start',function (data) {
var x = {id:hey.id, user:data.username, connect: true};
status.push(x);
});*/
hey.on('message', function (data) {
console.log(data);
});
io.emit('all',chat);
hey.on('disconnect', function () {
console.log('disconnected from server');
delete users[hey.id];
console.log('user disconnected'+hey.id);
/*
status.forEach(client=>{
if(hey.id===client.id){
client.connect = false;
}
})*/
//console.log(status);
})
hey.on('rec_message', function (data) {
//hey.emit will send message to that person
chat.push(data);
io.emit('get',data);
console.log(data+"is it so");
});
hey.on('users',function (data) {
users[hey.id] =data;
console.log(data,hey.id);
});
hey.on('del_message', function (data) {
//hey.emit will send message to that person
console.log("i am here ");
/*chat.forEach(client=>{
if(client.input===data){
delete chat[client];
}
});*/
for(let i = 0; i< chat.length;i++){
// console.log(chat.input);
if(chat[i].input===data){
chat.splice(i,1);
console.log("Test delete");
}
}
console.log(data+"Deleting message");
});
});
server.listen(port, function () {
console.log("server is running at 5000")
});