-
Notifications
You must be signed in to change notification settings - Fork 0
/
websoccon.js
45 lines (44 loc) · 1.76 KB
/
websoccon.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
var io = require('socket.io')();
var redis = require('redis');
var appconf = require('./appconf');
var generator = require('./generatorapp/generator');
console.log("socket file entered");
io.on('connection', function(socket) {
console.log("subscribing to client");
var redisClient = new redis.createClient(appconf.REDIS_PORT, appconf.REDIS_HOST);
socket.on("feed", function(datamodel) {
console.log("Request for feed");
datamodel = JSON.parse(datamodel);
generator.startGeneration(datamodel)
redisClient.subscribe('feed');
redisClient.on('message', function(channel, dataStr) {
var data = JSON.parse(dataStr);
emitterEventName = channel + "_" + data.email + '_' + data.name;
socket.emit(emitterEventName, data.data);
});
socket.on("disconnect", function() {
disconnectEvent = "feed_" + datamodel.email + '_' + datamodel.name + "_disconnect";
io.emit(disconnectEvent, "disconnecting")
});
});
socket.on("download", function(datamodel) {
datamodel = JSON.parse(datamodel)
generator.startGeneration(datamodel)
redisClient.subscribe('download')
redisClient.on('message', function(channel, dataStr) {
var data = JSON.parse(dataStr)
//console.log("getting the data in redis subscribe", data, "and channel is:", channel)
emitterEventName = channel + "_" + data.email + '_' + data.name;
socket.emit(emitterEventName, data.data)
});
socket.on("disconnect", function() {
disconnectEvent = "download_" + datamodel.email + '_' + datamodel.name + "_disconnect";
io.emit(disconnectEvent, "disconnecting")
});
});
socket.on("disconnect", function() {
redisClient.unsubscribe();
});
console.log("request on socket", socket.request.headers);
});
module.exports = io;