n2o.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // N2O CORE
  2. var active = false,
  3. debug = false,
  4. session = "site-sid",
  5. protocol = window.location.protocol == 'https:' ? "wss://" : "ws://",
  6. querystring = window.location.pathname + window.location.search,
  7. host = null == transition.host ? window.location.hostname : transition.host,
  8. port = null == transition.port ? window.location.port : transition.port;
  9. function N2O_start() {
  10. ws = new bullet(protocol + host + (port==""?"":":"+port) + "/ws" + querystring);
  11. ws.onmessage = function (evt) { // formatters loop
  12. for (var i=0;i<protos.length;i++) { p = protos[i]; if (p.on(evt, p.do).status == "ok") return; } };
  13. ws.onopen = function() { if (!active) { console.log('Connect'); ws.send('N2O,'+transition.pid); active=true; } };
  14. ws.onclose = function() { active = false; console.log('Disconnect'); }; next(); }
  15. function qi(name) { return document.getElementById(name); }
  16. function qs(name) { return document.querySelector(name); }
  17. function qn(name) { return document.createElement(name); }
  18. function is(x,num,name) { return x.t==106?false:(x.v.length === num && x.v[0].v === name); }
  19. function co(name) { match=document.cookie.match(new RegExp(name+'=([^;]+)')); return match?match[1]:undefined; }
  20. /// N2O Protocols
  21. var $io = {}; $io.on = function onio(r, cb) { if (is(r,3,'io')) {
  22. try { eval(utf8_dec(r.v[1].v)); if (typeof cb == 'function') cb(r); return { status: "ok" }; }
  23. catch (e) { console.log(e); return { status: '' }; } } else return { status: '' }; }
  24. var $file = {}; $file.on = function onfile(r, cb) { if (is(r,10,'ftp')) {
  25. if (typeof cb == 'function') cb(r); return { status: "ok" }; } else return { status: ''}; }
  26. var $bin = {}; $bin.on = function onbin(r, cb) { if (is(r,2,'bin')) {
  27. if (typeof cb == 'function') cb(r); return { status: "ok" }; } else return { status: '' }; }
  28. // BERT Formatter
  29. var $bert = {}; $bert.protos = [$io,$bin,$file]; $bert.on = function onbert(evt, cb) {
  30. if (Blob.prototype.isPrototypeOf(evt.data) && (evt.data.length > 0 || evt.data.size > 0)) {
  31. var r = new FileReader();
  32. r.addEventListener("loadend", function() {
  33. try { erlang = dec(r.result);
  34. if (debug) console.log(JSON.stringify(erlang));
  35. if (typeof cb == 'function') cb(erlang);
  36. for (var i=0;i<$bert.protos.length;i++) {
  37. p = $bert.protos[i]; if (p.on(erlang, p.do).status == "ok") return; }
  38. } catch (e) { console.log(e); } });
  39. r.readAsArrayBuffer(evt.data);
  40. return { status: "ok" }; } else return { status: "error", desc: "data" }; }
  41. var protos = [ $bert ];