-
Notifications
You must be signed in to change notification settings - Fork 76
/
mainserver_local.js
145 lines (103 loc) · 3.28 KB
/
mainserver_local.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
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
/**
* Module dependencies.
*/
var express = require('express')
, app = express()
, http = require('http')
, server = http.createServer(app)
, io = require('socket.io').listen(server);
var port = process.env.PORT || 5000;
server.listen(port);
//global variable to store input parameter
var connections = {};
var globalID = '';
function randomString(length) {
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'.split('');
if (! length) {
length = Math.floor(Math.random() * chars.length);
}
var str = '';
for (var i = 0; i < length; i++) {
str += chars[Math.floor(Math.random() * chars.length)];
}
return str;
}
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res) {
params = req.params.id;
res.sendfile(__dirname +'/views/index.html');
});
app.get('/:unique', function(req, res) {
params = req.params.unique;
res.sendfile(__dirname +'/views/post.html');
});
function getAndSendID(socket) {
var uniqueID = randomString(8);
if(typeof connections[uniqueID] === 'undefined') {
globalID = uniqueID;
//storing it in connections array
connections[uniqueID] = uniqueID;
socket.emit('youAreIn', {id:uniqueID})
return uniqueID;
} else {
getAndSendID();
}
}
io.sockets.on('connection', function(socket) {
socket.on('Test', function() {
console.log("TEST");
});
socket.emit('status',{data:"Connected"});
socket.on("getMeIn", function(data){
globalID = getAndSendID(socket);
io.of("/"+globalID).on('connection', function (socket) {
console.log(connections);
socket.on('stream', function(data) {
console.log("Stream Data Recived");
socket.volatile.broadcast.emit('op_stream', data);
});
socket.on('release', function(data) {
console.log(connections)
delete connections[data.id];
socket.volatile.broadcast.emit('no_connection', data);
socket.emit('disconnect_client');
console.log(connections)
});
});
});
});
// io.sockets.on('connection', function(socket) {
// //storing the room in the socket
// socket.room = params;
// console.log("Params: "+params);
// socket.join(params);
// socket.on('transmitter', function(data) {
// console.log("transmitter connectted.");
// socket.leave(params);
// socket.join('share');
// });
// socket.on('stream', function(data) {
// console.log("Stream Data Recived");
// io.sockets.in('share').emit('op_stream',data);
// socket.volatile.broadcast.emit('op_stream', data);
// });
// socket.on('disconnect', function() {
// console.log("disconnect called");
// socket.leave(socket.room);
// });
// });