n2o.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 = window.location.hostname,
  8. port = window.location.hostname == 'localhost' ? 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. };
  14. ws.onopen = function(){
  15. if(!active){
  16. console.log('Connect');
  17. ws.send('N2O,' + transition.pid);
  18. active = true;
  19. }
  20. };
  21. ws.onclose = function(){
  22. active = false;
  23. console.log('Disconnect');
  24. };
  25. next();
  26. }
  27. function qi(name){ return document.getElementById(name); }
  28. function qs(name){ return document.querySelector(name); }
  29. function qa(name){ return document.querySelectorAll(name); }
  30. function qn(name){ return document.createElement(name); }
  31. function is(x, num, name){ return x == undefined ? false : (x.t == 106 ? false : (x.v.length === num && x.v[0].v === name)); }
  32. function co(name){ match = document.cookie.match(new RegExp(name + '=([^;]+)')); return match ? match[1] : undefined; }
  33. /// N2O Protocols
  34. var $io = {};
  35. $io.on = function onio(r, cb){
  36. if(is(r, 3, 'io')){
  37. if(typeof cb == 'function') cb(r);
  38. var evalex = utf8_arr(r.v[1].v);
  39. try{
  40. console.log("from n2o.js:46 \n", evalex);
  41. eval(evalex);
  42. return { status: "ok" };
  43. }catch(e){
  44. console.error("Eval failed: \n", e);
  45. return { status: '' };
  46. }
  47. }else return { status: '' };
  48. };
  49. var $file = {};
  50. $file.on = function onfile(r, cb){
  51. //console.log('r ', r);
  52. //console.log('is ', is(r,13,'ftp'));
  53. if(is(r,13,'ftp')){
  54. if(typeof cb == 'function') cb(r);
  55. return { status: "ok" };
  56. }else return { status: ''};
  57. };
  58. var $bin = {};
  59. $bin.on = function onbin(r, cb){
  60. if(is(r,2,'bin')){
  61. if(typeof cb == 'function') cb(r);
  62. return { status: "ok" };
  63. }else return { status: '' };
  64. };
  65. // BERT Formatter
  66. var $bert = {};
  67. $bert.protos = [$io, $bin, $file];
  68. $bert.on = function onbert(evt, cb){
  69. if(ArrayBuffer.prototype.isPrototypeOf(evt.data) && (evt.data.byteLength > 0)){
  70. try{
  71. var erlang = dec(evt.data);
  72. //console.log(JSON.stringify(erlang));
  73. if(typeof cb == 'function') cb(erlang);
  74. for(var i = 0; i < $bert.protos.length; i++){
  75. p = $bert.protos[i];
  76. var ret = p.on(erlang, p.do);
  77. if(ret != undefined && ret.status == "ok") return ret;
  78. }
  79. }catch(e){ console.error(e); }
  80. return { status: "ok" };
  81. }else return { status: "error", desc: "data" };
  82. };
  83. var protos = [ $bert ];