ws_conn.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. function qi(name){ return document.getElementById(name); }
  2. function qs(name){ return document.querySelector(name); }
  3. function qa(name){ return document.querySelectorAll(name); }
  4. var active = false,
  5. token = "sess",
  6. protocol = window.location.protocol == 'https:' ? "wss://" : "ws://",
  7. //querystring = window.location.pathname + window.location.search,
  8. //querystring = window.location.pathname,
  9. querystring = window.location.pathname.substr(1), // rm heading "/"
  10. host = window.location.host,
  11. port = window.location.hostname == 'localhost' ? window.location.port : (window.transition ? window.transition.port : '');
  12. var heartbeat = null;
  13. var reconnectDelay = 1000;
  14. var maxReconnects = 1000;
  15. var not_reconnect = false;
  16. // WebSocket Transport
  17. var $ws = { heart: true, interval: 5000,
  18. creator: function(url) { return window.WebSocket ? new window.WebSocket(url) : false; },
  19. //onheartbeat: function() { this.channel.send('1'); } }; // send 'PING'
  20. onheartbeat: function() { this.channel.send( utf8_enc('1') ); } }; // send 'PING'
  21. // Reliable Connection
  22. var $conn = { onopen: nop, onmessage: nop, onclose: nop, onconnect: nop,
  23. send: function(data) { if (this.port.channel) this.port.channel.send(data); },
  24. close: function(manual) { if(this.port.channel){ clearInterval(heartbeat); if(manual === true){ not_reconnect = true } this.port.channel.close(); } } };
  25. function nop(){ }
  26. function bullet(url){ $conn.url = url; return $conn; }
  27. function reconnect(){ setTimeout(connect, reconnectDelay); }
  28. function next(){ $conn.port = $ws; return $conn.port ? connect() : false; }
  29. function connect(){
  30. $conn.port.channel = $conn.port.creator($conn.url);
  31. $conn.port.channel.binaryType = "arraybuffer";
  32. $conn.port.channel.onmessage = function(e){ $conn.onmessage(e); };
  33. $conn.port.channel.onopen = function(){
  34. if($conn.port.heart) heartbeat = setInterval(()=>{ $conn.port.onheartbeat(); }, $conn.port.interval);
  35. $conn.onopen();
  36. $conn.onconnect(); };
  37. $conn.port.channel.onclose = function(){ $conn.onclose(); clearInterval(heartbeat); if(not_reconnect){ not_reconnect = false; }else{ reconnect(); } };
  38. return $conn; }
  39. function ws_start(){
  40. //ws = new bullet(protocol + host + (port == "" ? "" : ":" + port) + "/ws" + querystring);
  41. ws = new bullet(protocol + host + (port == "" ? "" : ":" + port) + "/ws_" + querystring);
  42. ws.onmessage = function(evt){
  43. //console.log('evt: ', evt);
  44. if(evt.data === '' || evt.data === '0'){return}
  45. //for(var i = 0;i < protos.length; i++){ p = protos[i]; if(p.on(evt, p.do).status == "ok") return; }
  46. //if($bert.on(evt, $bert.do).status == "ok") return;
  47. try{
  48. eval(evt.data);
  49. }catch(e){
  50. console.error("Eval failed: \n", e);
  51. console.log('RESPONSE: ', evt.data);
  52. }
  53. };
  54. ws.onopen = function(){
  55. if(!active){
  56. active = true;
  57. console.log('ws Connect!');
  58. //ws.send('1');
  59. }
  60. };
  61. ws.onclose = function(){
  62. active = false;
  63. console.log('ws Disconnect!');
  64. };
  65. next();
  66. }
  67. /*
  68. var $io = {};
  69. $io.on = function onio(r, cb){
  70. if(is(r, 3, 'io')){
  71. if(typeof cb == 'function') cb(r);
  72. var evalex = utf8_arr(r.v[1].v);
  73. try{
  74. console.log("from n2o.js:46 \n", evalex);
  75. eval(evalex);
  76. return { status: "ok" };
  77. }catch(e){
  78. console.error("Eval failed: \n", e);
  79. return { status: '' };
  80. }
  81. }else return { status: '' };
  82. };
  83. var $file = {};
  84. $file.on = function onfile(r, cb){
  85. //console.log('r ', r);
  86. //console.log('is ', is(r,13,'ftp'));
  87. if(is(r,13,'ftp')){
  88. if(typeof cb == 'function') cb(r);
  89. return { status: "ok" };
  90. }else return { status: ''};
  91. };
  92. var $bin = {};
  93. $bin.on = function onbin(r, cb){
  94. if(is(r,2,'bin')){
  95. if(typeof cb == 'function') cb(r);
  96. return { status: "ok" };
  97. }else return { status: '' };
  98. };
  99. var $bert = {};
  100. //$bert.protos = [$io, $bin, $file];
  101. $bert.protos = [$io];
  102. $bert.on = function onbert(evt, cb){
  103. if(ArrayBuffer.prototype.isPrototypeOf(evt.data) && (evt.data.byteLength > 0)){
  104. try{
  105. var erlang = dec(evt.data);
  106. //console.log(JSON.stringify(erlang));
  107. if(typeof cb == 'function') cb(erlang);
  108. for(var i = 0; i < $bert.protos.length; i++){
  109. p = $bert.protos[i];
  110. var ret = p.on(erlang, p.do);
  111. if(ret != undefined && ret.status == "ok") return ret;
  112. }
  113. }catch(e){ console.error(e); }
  114. return { status: "ok" };
  115. }else return { status: "error", desc: "data" };
  116. };
  117. //var protos = [ $bert ];
  118. */