-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
65 lines (61 loc) · 1.51 KB
/
socket.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
"use strict";
const _ = require("lodash");
module.exports = function (app) {
let userNumber = 0;
let user = [];
let messages = [];
let sockets = {};
app.io.use(function* (next) {
console.log("connect");
yield* next;
console.log("close");
const username = this.username;
_.remove(user, function (n) {
return n == username;
});
--userNumber;
this.broadcast.emit('user leave', {
username: username,
userNumber: userNumber,
user: user
});
this.emit('user leave', {
username: username,
userNumber: userNumber,
user: user
});
});
app.io.route('new user', function* (next, username) {
sockets[username] = this;
this.username = username;
if (_.some(user, function (n) {
return n == username;
})) {
this.emit('add fail');
} else {
user.push(username);
userNumber = userNumber + 1;
this.broadcast.emit('add user', {
username: username,
userNumber: userNumber,
user: user
});
this.emit('add user', {
username: username,
userNumber: userNumber,
user: user
});
this.emit('send', messages);
this.broadcast.emit('send', messages);
}
});
app.io.route('send', function* (next, message) {
messages.push(message);
this.emit('send', messages);
this.broadcast.emit('send', messages);
});
app.io.route('video', function* (next, username) {
let socket = sockets[username];
socket.emit("video");
});
};