-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (142 loc) · 3.84 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('./app');
var debug = require('debug')('projectbolt:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
var io = require('socket.io')(server);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log("Server running at http://localhost:%d", port); // New
debug('Listening on ' + bind);
}
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
// Notification web socket namespace
var notificationSocket = io.of('/notifications');
notificationSocket.on('connection', function(socket){
console.log('Someone subscribed to notifications!');
socket.on('newQuestion', function (data) {
socket.broadcast.emit('newQuestion', data);
});
socket.on('newAnswer', function (data) {
socket.broadcast.emit('newAnswer', data);
});
socket.on('disconnect', function(){
console.log('Someone unsubscribed from notifications');
});
});
// Chat web socket namespace
var chatSocket = io.of('/chat');
var chatUsers = []; // users chatting
chatSocket.on('connection', function(socket){
//default username
socket.username = "Anonymous"
console.log(socket.username + " connected to the chat");
//listen to changeUsername
socket.on('changeUsername', (data) => {
console.log(socket.username + " changed name to " + data.username);
socket.username = data.username
chatUsers.push(socket.username); // Add user to active users
chatSocket.emit('updateUsers', {"activeUsers": chatUsers.toString()});
})
//listen on new_message
socket.on('new_message', (data) => {
var transmit = {date : new Date().toISOString(), username : socket.username, message : data.message};
chatSocket.emit('new_message', transmit);
console.log("User "+ transmit['username'] +" said \""+transmit['message']+"\""+" at " + getTime());
})
//listen on typing
socket.on('typing', (data) => {
socket.broadcast.emit('typing', {username : socket.username})
})
socket.on('disconnect', function () {
console.log(socket.username + " disconnected from the chat");
var userIndex = chatUsers.indexOf(socket.username);
if (userIndex > -1) {
chatUsers.splice(userIndex, 1);
}
chatSocket.emit('updateUsers', {"activeUsers": chatUsers.toString()});
})
});
function getTime(){
var today = new Date();
var mi = today.getMinutes();
var hr = today.getHours();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(mi<10) {
mi = '0'+dd
}
if(hr<10) {
hr = '0'+mm
}
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = "["+hr+":"+mi+"] " + mm + '/' + dd + '/' + yyyy;
return today
}