n4u.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // N4U 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 N4U_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('N4U,'+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. /// N4U Protocols
  21. var $io = {}; $io.on = function onio(r, cb) { if (is(r,3,'io')) {
  22. try {
  23. if(window.debug){ console.log("from n4u.js:29\n", utf8_dec(r.v[1].v)); }
  24. eval(utf8_dec(r.v[1].v)); if (typeof cb == 'function') cb(r); return { status: "ok" };
  25. } catch (e) { console.log(e); return { status: '' }; } } else return { status: '' }; }
  26. var $file = {}; $file.on = function onfile(r, cb) {
  27. //console.log('r ', r);
  28. //console.log('is ', is(r,13,'ftp'));
  29. if (is(r,13,'ftp')) {
  30. if (typeof cb == 'function') cb(r); return { status: "ok" }; } else return { status: ''}; }
  31. var $bin = {}; $bin.on = function onbin(r, cb) { if (is(r,2,'bin')) {
  32. if (typeof cb == 'function') cb(r); return { status: "ok" }; } else return { status: '' }; }
  33. // BERT Formatter
  34. var $bert = {}; $bert.protos = [$io,$bin,$file]; $bert.on = function onbert(evt, cb) {
  35. if (Blob.prototype.isPrototypeOf(evt.data) && (evt.data.length > 0 || evt.data.size > 0)) {
  36. var r = new FileReader();
  37. r.addEventListener("loadend", function() {
  38. try { erlang = dec(r.result);
  39. if (debug) console.log(JSON.stringify(erlang));
  40. if (typeof cb == 'function') cb(erlang);
  41. for (var i=0;i<$bert.protos.length;i++) {
  42. p = $bert.protos[i]; if (p.on(erlang, p.do).status == "ok") return; }
  43. } catch (e) { console.log(e); } });
  44. r.readAsArrayBuffer(evt.data);
  45. return { status: "ok" }; } else return { status: "error", desc: "data" }; }
  46. var protos = [ $bert ];