-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotification.server.js
59 lines (51 loc) · 1.71 KB
/
notification.server.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
/**
* TODO:
* 1. Perkelti Notification serveri i atskira repo.
* 2. Sutvarkyti bendra struktura. Isskaidyti i atskirus failus/servisus.
*/
var fs = require('fs');
var express = require('express');
var app = express();
var https = require('https');
var keys = {
key: fs.readFileSync('/etc/nginx/ssl/maze.lt/87263/server.key'),
cert: fs.readFileSync('/etc/nginx/ssl/maze.lt/87263/server.crt')
};
var server = https.createServer(keys, app).listen(6001);
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();
var redisServer = new Redis();
var Imagemin = require('imagemin');
var path = require('path');
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'https://maze.lt');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST', 'PATCH', 'PUT', 'DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
redis.psubscribe('*', function(err, count) {
if(err) console.log(err);
});
redis.on('pmessage', function(subscribed, channel, event) {
event = JSON.parse(event);
io.sockets.in(event.data.channel).emit(channel, event.data.data);
});
/**
* Kiekvienas connection turi savo scope.
* Viskas, kas yra connection funkcijoje yra kuriama atskirai kiekvienam socketo prisijungimui.
* Potencialiai gali valgyti daug atminties.
*/
io.sockets.on('connection', function (socket) {
var secret;
// Prisijungimas į savo secret kanalą.
socket.on('join', function(channel) {
secret = channel;
socket.join(channel);
redisServer.sadd('online_users', secret);
});
socket.on('disconnect', function() {
redisServer.srem('online_users', secret);
});
});