ws_conn.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. console.log('evt.data: ', evt.data);
  49. eval(evt.data);
  50. }catch(e){
  51. console.error("Eval failed: \n", e);
  52. console.log('RESPONSE: ', evt.data);
  53. }
  54. };
  55. ws.onopen = function(){
  56. if(!active){
  57. active = true;
  58. console.log('ws Connect!');
  59. //ws.send('1');
  60. }
  61. };
  62. ws.onclose = function(){
  63. active = false;
  64. console.log('ws Disconnect!');
  65. };
  66. next();
  67. }
  68. /*
  69. var $io = {};
  70. $io.on = function onio(r, cb){
  71. if(is(r, 3, 'io')){
  72. if(typeof cb == 'function') cb(r);
  73. var evalex = utf8_arr(r.v[1].v);
  74. try{
  75. console.log("from n2o.js:46 \n", evalex);
  76. eval(evalex);
  77. return { status: "ok" };
  78. }catch(e){
  79. console.error("Eval failed: \n", e);
  80. return { status: '' };
  81. }
  82. }else return { status: '' };
  83. };
  84. var $file = {};
  85. $file.on = function onfile(r, cb){
  86. //console.log('r ', r);
  87. //console.log('is ', is(r,13,'ftp'));
  88. if(is(r,13,'ftp')){
  89. if(typeof cb == 'function') cb(r);
  90. return { status: "ok" };
  91. }else return { status: ''};
  92. };
  93. var $bin = {};
  94. $bin.on = function onbin(r, cb){
  95. if(is(r,2,'bin')){
  96. if(typeof cb == 'function') cb(r);
  97. return { status: "ok" };
  98. }else return { status: '' };
  99. };
  100. var $bert = {};
  101. //$bert.protos = [$io, $bin, $file];
  102. $bert.protos = [$io];
  103. $bert.on = function onbert(evt, cb){
  104. if(ArrayBuffer.prototype.isPrototypeOf(evt.data) && (evt.data.byteLength > 0)){
  105. try{
  106. var erlang = dec(evt.data);
  107. //console.log(JSON.stringify(erlang));
  108. if(typeof cb == 'function') cb(erlang);
  109. for(var i = 0; i < $bert.protos.length; i++){
  110. p = $bert.protos[i];
  111. var ret = p.on(erlang, p.do);
  112. if(ret != undefined && ret.status == "ok") return ret;
  113. }
  114. }catch(e){ console.error(e); }
  115. return { status: "ok" };
  116. }else return { status: "error", desc: "data" };
  117. };
  118. //var protos = [ $bert ];
  119. */