-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
76 lines (69 loc) · 2.58 KB
/
index.js
File metadata and controls
76 lines (69 loc) · 2.58 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
function Broadcaster(ip, port) {
var broadcaster = this;
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var domain = require('domain');
var dio = domain.create();
dio.on('error', function (err) {
log('socket io domain:', err.stack);
});
var log = require('debug')('JGF:index');
log('=======');
log('Ya hagh');
log('-------');
var Client = require('./app/Client');
broadcaster.app = app;
broadcaster.express = express;
broadcaster.start = function (port) {
server.listen(port);
};
broadcaster.static = function (dir) {
app.use(express.static(dir));
};
broadcaster.run = function (serverId, token) {
var channel = '/server' + serverId;
var client = new Client(io.of(channel), ip, port + serverId, token);
dio.run(function () {
io
.of(channel)
.on('connection', function (socket) {
var socketViews = {};
socket.join('_clients');
socket.emit('info', client.getInfo());
socket.emit('map', client.getMap());
socket.emit('status', client.getStatus());
socket.on('join', function (view, fn) {
log('join', view);
fn = (typeof fn === 'function') ? fn : function () {
};
var views = client.getViews();
if (views[view]) {
socket.join(view);
socketViews[view] = true;
var data = client.getViewData(view);
fn({ok: true});
socket.emit('diff', data);
} else {
fn({ok: false});
}
});
socket.on('leave', function (view, fn) {
log('leave', view);
fn = (typeof fn === 'function') ? fn : function () {
};
if (socketViews[view]) {
socket.leave(view);
delete socketViews[view];
fn({ok: true});
} else {
fn({ok: false});
}
});
});
});
return client;
}
}
module.exports = Broadcaster;