mirror of https://github.com/calzoneman/sync.git
More work on uws integration
This commit is contained in:
parent
fb5f92b7ee
commit
1021cc706a
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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.",
|
||||
|
|
|
@ -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;
|
||||
})();
|
Loading…
Reference in New Issue