-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws_service.js
executable file
·108 lines (93 loc) · 3.75 KB
/
ws_service.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
const debug = require('debug')('ws');
const sharedsession = require('express-socket.io-session');
let users = require('./users');
module.exports = (server, session) => {
const io = require('socket.io').listen(server);
let connections = [];
io.use(sharedsession(session, {
autoSave: true,
}));
io.use(function (socket, next) {
debug('check request.user %s', JSON.stringify(socket.request.user));
debug('check request.session %s', JSON.stringify(socket.request.session));
debug('check handshake %s', JSON.stringify(socket.handshake, null, 2));
debug('check cookie %s', JSON.stringify(socket.request.headers));
debug('check socket.id %s', JSON.stringify(socket.id));
//session(socket.request, socket.request.res, next);
next();
});
io.sockets.on('connection', (socket) => {
updateLocations = () => {
let locations = []
connections.forEach((s) => {
if (s.username && s.location) {
//debug('username=%s, location=%s %s', s.username, s.location.lat, s.location.lng);
locations.push({ username: s.username, location: s.location });
}
});
debug('updateLocations(): len=%d, %s', locations.length, JSON.stringify(locations, null, ' '));
if (locations.length > 0) {
io.sockets.emit('update-locations', locations);
}
};
updateUsers = () => {
let usernames = [];
connections.forEach((s) => {
if (s.username) {
usernames.push(s.username);
}
});
debug('updateUsers(): len=%d, %s', usernames.length, JSON.stringify(usernames, null, ' '));
if (usernames.length > 0) {
io.sockets.emit('update-users', usernames);
}
};
connections.push(socket);
let passport = socket.handshake.session.passport;
if (passport) {
// check from user DB, can be replaced with any kind of DB.
let user = users.filter((u) => {
debug('u id:' + u.id)
debug('passport id:' + passport.user.id)
return u.id == passport.user.id;
});
if (user.length == 1) {
debug('update-username ' + user[0].username);
socket.emit('update-username', user[0].username);
socket.username = user[0].username;
updateUsers();
}
}
debug('Connected: %s socket connections', connections.length);
socket.on('disconnect', (data) => {
//if (!socket.username) return;
connections.splice(connections.indexOf(socket), 1);
updateUsers();
updateLocations();
debug('Disconnected: %s socket connections', connections.length);
});
socket.on('new-user', (data) => {
debug('new-user: %s', data);
socket.username = data;
updateUsers();
updateLocations();
});
socket.on('send-message', (data) => {
if (socket.username === undefined) {
debug('no username!');
return;
}
debug('new message: %s from %s', data, socket.username);
io.sockets.emit('new-message', { username: socket.username, msg: data });
});
socket.on('send-location', (data) => {
if (socket.username === undefined) {
debug('no username!');
return;
}
debug('send-location: %s', JSON.stringify(data, null, ' '));
socket.location = data;
updateLocations();
});
});
};