UNPKG

luvit-zed

Version:

Util-library for Luvit.io

156 lines (129 loc) 3.85 kB
var xmlhttp; if (window.XMLHttpRequest){ // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else{ // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } encodeTable = function(t){ if (typeof t === 'object' || typeof t === 'array'){ for(k in t){ if (typeof t[k] === 'object' || typeof t[k] === 'array'){ t[btoa(k.toString())] = t[k]; delete t[k]; encodeTable(t[btoa(k.toString())]); }else{ t[btoa(k.toString())] = btoa(t[k].toString()); delete t[k]; } } } } decodeTable = function(t){ if (typeof t === 'object' || typeof t === 'array'){ for(k in t){ if (typeof t[k] === 'object' || typeof t[k] === 'array'){ t[atob(k.toString())] = t[k]; delete t[k]; decodeTable(t[atob(k.toString())]); }else{ t[atob(k.toString())] = atob(t[k].toString()); delete t[k]; } } } } function Zed_AJAX(self){ xmlhttp.open("GET", "http://" + self.host + ":" + self.port + "/connect?t=" + Math.random(), false) xmlhttp.send(); self.uid = xmlhttp.responseText; self.packets = {}; xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 && xmlhttp.status==200 && xmlhttp.responseText && xmlhttp.responseText.length > 4) { var data = JSON.parse(xmlhttp.responseText); for(var i = 0; i < data.length; i++){ decodeTable(data[i]); if(self.packets[data[i].packet]){ self.packets[data[i].packet](data[i].data) } } }else{ if(self.packets["disconnect"]){ self.packets["disconnect"]() } } } self.send = function(packet, _data){ var data = {packet: packet, data: _data}; encodeTable(data); xmlhttp.open("GET", "http://" + host + "/send?t=" + Math.random() + "&id=" + self.uid + "&data=" + JSON.stringify(data), true); xmlhttp.send(); } self.on = function(packet, cb){ self.packets[packet] = cb; } setInterval(function () { xmlhttp.open("GET","http://" + host + "/get?t=" + Math.random() + "&id=" + self.uid, true); xmlhttp.send(); }, 500); window.onbeforeunload = function(e) { xmlhttp.open("GET", "http://" + host + "/disconnect?t=" + Math.random() + "&id=" + self.uid, true); xmlhttp.send(); }; return self; } function Zed_WS(self){ self.listener = {}; self.connectionHandlers = {}; var ws = new WebSocket("ws://" + self.host + ":" + (self.port + 1) + "/"); ws.onmessage = function (evt) { var msg = evt.data; var data = JSON.parse(msg); decodeTable(data); if(self.listener[data.packet]){ self.listener[data.packet](data.data); } }; ws.onopen = function() { for(i in self.connectionHandlers){ self.connectionHandlers[i](); } }; ws.onclose = function(){ if(self.listener["disconnect"]){ self.listener["disconnect"]() } }; self.send = function(packet, _data){ var data = {packet: packet, data: _data}; encodeTable(data); ws.send(JSON.stringify(data)); }; self.on = function(packet, cb){ self.listener[packet] = cb; }; self.onConnection = function(func){ if (typeof(func) === "function") { self.connectionHandlers[self.connectionHandlers.length] = func } }; window.onbeforeunload = function(e) { var data = {packet: "disconnect", data: {}}; encodeTable(data); ws.send(JSON.stringify(data)); }; return self; } function ZedSocket(host, port, wsOnly){ var self = this; self.host = host; self.port = port; if ("WebSocket" in window) return Zed_WS(self); else if(!wsOnly) return Zed_AJAX(self); }