diff --git a/src/io/uws.js b/src/io/uws.js index b4c85eee..4b4c6f98 100644 --- a/src/io/uws.js +++ b/src/io/uws.js @@ -92,15 +92,13 @@ export function init() { const server = new uws.Server({ port: 3000 }); server.on('connection', socket => { - const wrap = new UWSWrapper( - socket, - { - aliases: [], - ipSessionFirstSeen: new Date(), - torConnection: false, - ipAddress: null - } - ); + const context = { + aliases: [], + ipSessionFirstSeen: new Date(), + torConnection: false, + ipAddress: null + }; + const wrap = new UWSWrapper(socket, context); new User(wrap, '127.0.0.1', null); }); } diff --git a/templates/channel.pug b/templates/channel.pug index 89e38daf..083e3c0d 100644 --- a/templates/channel.pug +++ b/templates/channel.pug @@ -247,6 +247,7 @@ html(lang="en") script(src="/js/player.js") script(src="/js/paginator.js") script(src="/js/ui.js") + script(src="/js/ws.js") script(src="/js/callbacks.js") script(defer, src="https://www.youtube.com/iframe_api") script(defer, src="https://api.dmcdn.net/all.js") diff --git a/www/js/callbacks.js b/www/js/callbacks.js index 1d7b3b10..d9c60199 100644 --- a/www/js/callbacks.js +++ b/www/js/callbacks.js @@ -1295,10 +1295,16 @@ function checkLetsEncrypt(socketConfig, nonLetsEncryptError) { }); } +function initWS() { + window.socket = new WSShim(new WebSocket('ws://localhost:3000/')); + setupCallbacks(); +} + (function () { $.getJSON("/socketconfig/" + CHANNEL.name + ".json") .done(function (socketConfig) { - initSocketIO(socketConfig); + //initSocketIO(socketConfig); + initWS(); }).fail(function () { makeAlert("Error", "Failed to retrieve socket.io configuration. " + "Please try again in a few minutes.", diff --git a/www/js/ws.js b/www/js/ws.js new file mode 100644 index 00000000..807fee4d --- /dev/null +++ b/www/js/ws.js @@ -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; +})();