This repository has been archived by the owner on Aug 11, 2024. It is now read-only.
forked from blakjakau/chromote
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrealtime.js
197 lines (176 loc) · 6.11 KB
/
realtime.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
module.exports = function(http, config) {
var hal;
//console.log('Loading the xdo HAL module'); hal = require('./hal/xdo.js');
console.log('Loading the uinput HAL module'); hal = require('./hal/uinput.js');
var WebSocketServer = require('websocket').server;
var socket = new WebSocketServer({
httpServer:http,
autoAcceptConnections: false
});
var session = require('cookie-session');
var connectionID = 0;
var messageID = 0;
var ws = {
connections: [], // track our connections!
lastEvent: 0,
keysDown:{},
parse: function(message, connection) {
if(message.id != connection.serialID) {
connection.pending.push({
id: message.id,
message: message,
connection, connection
});
connection.pending.sort((a,b)=>{
return a.id - b.id;
});
if(connection.pending[0].id==connection.serialID) {
ws.parse(connection.pending.shift(), connection);
}
// console.log(connection.id, connection.pending[0].id, connection.serialID);
/// some failsafe here to flush the buffer if a message is actually lost?
return;
}
//console.log(connection.serialID, message.id, message.type, message.data);
connection.serialID++;
// assuming the message is a valid object...
// if we got here, it means that the connection has already constructed an object for us.
//console.log(message, connection.id);
if(message.type) {
switch(message.type) {
case "clearInputState": hal.clearInput(); break;
case "input": hal.process(message.data); break;
}
}
connection.pending.sort((a,b)=>{
return b.id - a.id;
});
if(connection.pending[0]==connection.serialID) {
ws.parse(connection.pending.shift(), connection);
}
},
// replicate message to all live ADMIN class clients (Except the source)
adminBroadcast: function(message, from) {
ws.connections.forEach(function(conn) {
if(from && conn.id == from.id) return; // skip the source
if(conn.user && conn.user.groups && conn.user.groups.indexOf('admin')==-1 && conn.user.groups.indexOf('dev')==-1) return;
if(conn.connected) {
conn.sendUTF(JSON.stringify({
client: (from && from.id?from.id:0),
data: message
}));
}
});
},
eventBroadcast: function(message) {
ws.connections.forEach(function(conn) {
if(conn.connected) {
conn.sendUTF(JSON.stringify({
client: 'server',
data: message
}));
}
});
},
// replicate message to all live clients (Except the source)
broadcast: function(message, from) {
ws.connections.forEach(function(conn) {
if(from && conn.id == from.id) return; // skip the source
if(conn.connected) {
conn.sendUTF(JSON.stringify({
client: (from && from.id?from.id:0),
data: message
}));
}
});
},
send: function(message, conn) {
conn.sendUTF(JSON.stringify({
client: "server",
data: message
}));
},
handleRequest: function(request) {
// because websockets are seperate to their upgraded http request
// we need to re-parse the session stuff. here we are just repeating
// the session code we gave express, so it *should* be seamless
session({ name:'share-Session', secret: 'omg...things', signed: false })(request.httpRequest, {}, function() { });
var origins = ['http://localhost:27131', "https://build.jakbox.net"];
// if(origins.indexOf(request.origin)==-1) {
// request.reject();
// console.log((new Date()) + ' Connection from origin ' + request.origin + ' rejected.');
// return;
// }
//oauth2.aware(request.httpRequest);
// console.log(request.httpRequest.headers);
var connection = request.accept('software-router-rtc', request.origin);
// store the session state on the connection object for future reference.
connection.session = request.httpRequest.session;
connection.user = connection.session.user;
connection.ua = request.userAgent;
connection.url = connection.session.lastPath;
console.log("WS:", request.httpRequest.headers['user-agent']);
connectionID++;
connection.id = connectionID;
connection.pending = [];
connection.serialID = 1;
connection.started = new Date();
connection.userAgentString = request.httpRequest.headers['user-agent']
ws.connections.push(connection);
console.log((new Date()) + ' Connection accepted.', ws.connections.length);
ws.adminBroadcast({
type:'client.connect',
message:'Client '+connection.id+' connected.',
clients:ws.connections.length,
data: {
id: connection.id,
url: connection.url,
user: connection.user,
ua: connection.userAgentString,
timestamp: connection.started
}
}, {id:0});
// store an array of connections
connection.on('message', function(message) {
if (message.type === 'utf8') {
try {
ws.parse(JSON.parse(message.utf8Data), connection);
} catch(e) {
console.log((new Date())+" Failed to process message from "+connection.id);
console.log("ERROR:\n"+e+"\n");
console.log("DATA:\n"+message.utf8Data+"\n");
}
}
else if (message.type === 'binary') {
console.log('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes(message.binaryData);
}
});
ws.send({ type:'host.control', readystate:1 }, connection);
connection.on('close', function(reasonCode, description) {
// update the session model, in case we don't load another page
// make sure we're onyl updating the session model outsite the router...
console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
ws.adminBroadcast({
type:'client.disconnect',
message:'Client '+connection.id+' disconnected.',
clients:ws.connections.length-1,
data: {
id: connection.id
}
}, {id:0});
for(var i=0,l=ws.connections.length; i<l; i++){
if(ws.connections[i].id == connection.id) {
console.log(new Date() + " connections: "+ws.connections.length);
ws.connections.splice(i, 1);
return;
}
}
console.log(new Date() + " connections: "+ws.connections.length);
});
}
}
socket.on('request', ws.handleRequest);
hal.handler = ws;
return ws;
}