1
0
Fork 0
mirror of https://github.com/FrankerFaceZ/FrankerFaceZ.git synced 2025-06-27 21:05:53 +00:00
FrankerFaceZ/src/socket.js

93 lines
No EOL
2.1 KiB
JavaScript

var FFZ = window.FrankerFaceZ;
FFZ.prototype._ws_open = false;
FFZ.prototype._ws_delay = 0;
FFZ.ws_commands = {};
// ----------------
// Socket Creation
// ----------------
FFZ.prototype.ws_create = function() {
var f = this;
this._ws_last_req = 0;
this._ws_callbacks = {};
var ws = this._ws_sock = new WebSocket("ws://ffz.stendec.me/");
ws.onopen = function(e) {
f._ws_open = true;
f._ws_delay = 0;
f.log("Socket connected.");
var user = f.get_user();
if ( user )
f.ws_send("setuser", user.login);
// Send the current rooms.
for(var room_id in f.rooms)
f.ws_send("sub", room_id);
}
ws.onclose = function(e) {
f.log("Socket closed.");
f._ws_open = false;
// We never ever want to not have a socket.
if ( f._ws_delay < 30000 )
f._ws_delay += 5000;
setTimeout(f.ws_create.bind(f), f._ws_delay);
}
ws.onmessage = function(e) {
// Messages are formatted as REQUEST_ID SUCCESS/FUNCTION_NAME[ JSON_DATA]
var cmd, data, ind = e.data.indexOf(" "),
msg = e.data.substr(ind + 1),
request = parseInt(e.data.slice(0, ind));
ind = msg.indexOf(" ");
if ( ind === -1 )
ind = msg.length;
cmd = msg.slice(0, ind);
msg = msg.substr(ind + 1);
if ( msg )
data = JSON.parse(msg);
if ( request === -1 ) {
// It's a command from the server.
var command = FFZ.ws_commands[cmd];
if ( command )
command.bind(f)(data);
else
f.log("Invalid command: " + cmd, data);
} else {
var success = cmd === 'True',
callback = f._ws_callbacks[request];
f.log("Socket Reply to " + request + " - " + (success ? "SUCCESS" : "FAIL"), data);
if ( callback ) {
delete f._ws_callbacks[request];
callback(success, data);
}
}
}
}
FFZ.prototype.ws_send = function(func, data, callback) {
if ( ! this._ws_open ) return false;
var request = ++this._ws_last_req;
data = data !== undefined ? " " + JSON.stringify(data) : "";
if ( callback )
this._ws_callbacks[request] = callback;
this._ws_sock.send(request + " " + func + data);
return request;
}