More work on uws integration

This commit is contained in:
Calvin Montgomery 2018-06-14 21:33:40 -07:00
parent fb5f92b7ee
commit 1021cc706a
4 changed files with 84 additions and 10 deletions

View File

@ -92,15 +92,13 @@ export function init() {
const server = new uws.Server({ port: 3000 }); const server = new uws.Server({ port: 3000 });
server.on('connection', socket => { server.on('connection', socket => {
const wrap = new UWSWrapper( const context = {
socket, aliases: [],
{ ipSessionFirstSeen: new Date(),
aliases: [], torConnection: false,
ipSessionFirstSeen: new Date(), ipAddress: null
torConnection: false, };
ipAddress: null const wrap = new UWSWrapper(socket, context);
}
);
new User(wrap, '127.0.0.1', null); new User(wrap, '127.0.0.1', null);
}); });
} }

View File

@ -247,6 +247,7 @@ html(lang="en")
script(src="/js/player.js") script(src="/js/player.js")
script(src="/js/paginator.js") script(src="/js/paginator.js")
script(src="/js/ui.js") script(src="/js/ui.js")
script(src="/js/ws.js")
script(src="/js/callbacks.js") script(src="/js/callbacks.js")
script(defer, src="https://www.youtube.com/iframe_api") script(defer, src="https://www.youtube.com/iframe_api")
script(defer, src="https://api.dmcdn.net/all.js") script(defer, src="https://api.dmcdn.net/all.js")

View File

@ -1295,10 +1295,16 @@ function checkLetsEncrypt(socketConfig, nonLetsEncryptError) {
}); });
} }
function initWS() {
window.socket = new WSShim(new WebSocket('ws://localhost:3000/'));
setupCallbacks();
}
(function () { (function () {
$.getJSON("/socketconfig/" + CHANNEL.name + ".json") $.getJSON("/socketconfig/" + CHANNEL.name + ".json")
.done(function (socketConfig) { .done(function (socketConfig) {
initSocketIO(socketConfig); //initSocketIO(socketConfig);
initWS();
}).fail(function () { }).fail(function () {
makeAlert("Error", "Failed to retrieve socket.io configuration. " + makeAlert("Error", "Failed to retrieve socket.io configuration. " +
"Please try again in a few minutes.", "Please try again in a few minutes.",

69
www/js/ws.js Normal file
View File

@ -0,0 +1,69 @@
(function () {
function WSShim(ws) {
this._ws = ws;
this._listeners = Object.create(null);
this._ws.onopen = this._onopen.bind(this);
this._ws.onclose = this._onclose.bind(this);
this._ws.onmessage = this._onmessage.bind(this);
this._ws.onerror = this._onerror.bind(this);
}
WSShim.prototype.listeners = function listeners(frame) {
if (!Object.prototype.hasOwnProperty.call(this._listeners, frame)) {
this._listeners[frame] = [];
}
return this._listeners[frame];
};
WSShim.prototype.on = function on(frame, callback) {
this.listeners(frame).push(callback);
};
WSShim.prototype.emit = function emit(/* args */) {
var args = Array.prototype.slice.call(arguments).filter(function (it) {
// TODO: handle ack
return typeof it !== 'function';
});
this._ws.send(JSON.stringify(args));
};
WSShim.prototype._emit = function _emit(frame /*, args */) {
var args = Array.prototype.slice.call(arguments, 1);
this.listeners(frame).forEach(function (cb) {
cb.apply(null, args);
});
};
WSShim.prototype._onopen = function _onopen() {
this._emit('connect');
};
WSShim.prototype._onclose = function _onclose() {
// TODO: reconnect logic
this._emit('disconnect');
};
WSShim.prototype._onmessage = function _onmessage(message) {
var args;
try {
args = JSON.parse(message.data);
} catch (error) {
console.error('Unparseable message from server: ' + message);
console.error(error.stack);
return;
}
this._emit.apply(this, args);
};
WSShim.prototype._onerror = function _onerror() {
console.error('Dunno how to handle onerror');
};
window.WSShim = WSShim;
})();