-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·157 lines (121 loc) · 4.31 KB
/
app.js
File metadata and controls
executable file
·157 lines (121 loc) · 4.31 KB
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
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
var app = module.exports = express.createServer()
, io = require('socket.io').listen(app);
//redis
var redis = require("redis"),
publisher = redis.createClient();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
//Testing modules
var participants_module = require('./bbb_modules/participants');
participants_module.hello();
//Rooms
var rooms = {};
// Routes
app.get('/', routes.index);
app.get('/join',routes.join);
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
io.sockets.on('connection', function (socket) {
subscriber = redis.createClient();
subscriber.on("pmessage",function (pattern, channel, message) {
var obj = eval('(' + message + ')');
var username = obj["username"];
var meetingID = obj["meetingID"];
var evt = obj["event"];
var protocol = obj["protocol"];
if(channel == "bigbluebutton:bridge:participants"){
//checking if the event is join
if(evt == "join"){
// if room doesn't exist create it...
if(rooms[meetingID] == undefined){
rooms[meetingID] = {}
}
// add the client's username to the room
rooms[meetingID][username] = username;
if(socket.username == undefined){
if(protocol == "websockets"){
// we store the username and the room in the socket session for this client
socket.username = username;
socket.room = meetingID;
//join a socket to a room
socket.join(meetingID);
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected to ' + meetingID);
}
}
// echo to the room that a person has connected
socket.broadcast.to(meetingID).emit('updatechat', 'SERVER', username + ' has connected');
// update the list of users in chat, client-side
socket.to(meetingID).emit('updateusers', rooms[meetingID]);
}
else if(evt == "leave"){
// remove the username from global usernames list
delete rooms[meetingID][username];
//delete room if the room is empty
if(Object.keys(rooms[meetingID]).length == 0){
delete rooms[meetingID];
}
// update list of users in chat, client-side
io.sockets.to(meetingID).emit('updateusers', rooms[meetingID]);
// echo globally that this client has left
socket.broadcast.to(meetingID).emit('updatechat', 'SERVER', username + ' has disconnected');
}
}else if(channel == "bigbluebutton:bridge:chat"){
if(evt == "newMessage"){
var message = obj["message"];
// we tell the client to execute 'updatechat' with 2 parameters
socket.to(meetingID).emit('updatechat', username, message);
}
}
});
subscriber.psubscribe("bigbluebutton:*");
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (data) {
console.log("test: "+socket.username)
var obj = {};
obj["username"] = socket.username;
obj["meetingID"] = socket.room;
obj["event"] = "newMessage";
obj["protocol"] = "websockets";
obj["message"] = data;
publisher.publish("bigbluebutton:bridge:chat", JSON.stringify(obj));
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username, meetingID){
//Add the user to the list using redis pubsub
var obj = {};
obj["username"] = username;
obj["meetingID"] = meetingID;
obj["event"] = "join";
obj["protocol"] = "websockets";
publisher.publish("bigbluebutton:bridge:participants", JSON.stringify(obj));
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
//Remove the user using redis pubsub
var obj = {};
obj["username"] = socket.username;
obj["meetingID"] = socket.room;
obj["event"] = "leave";
obj["protocol"] = "websockets";
publisher.publish("bigbluebutton:bridge:participants", JSON.stringify(obj));
});
});