meshcentral.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @fileoverview Meshcentral.js
  3. * @author Ylian Saint-Hilaire
  4. * @version v0.0.1
  5. */
  6. var MeshServerCreateControl = function (domain, authCookie) {
  7. var obj = {};
  8. obj.State = 0;
  9. obj.connectstate = 0;
  10. obj.pingTimer = null;
  11. obj.authCookie = authCookie;
  12. //obj.trace = false;
  13. obj.xxStateChange = function (newstate, errCode) {
  14. if (obj.State == newstate) return;
  15. var previousState = obj.State;
  16. obj.State = newstate;
  17. if (obj.onStateChanged) obj.onStateChanged(obj, obj.State, previousState, errCode);
  18. }
  19. obj.Start = function () {
  20. if (obj.connectstate != 0) return;
  21. obj.connectstate = 0;
  22. var url = window.location.protocol.replace('http', 'ws') + '//' + window.location.host + domain + 'control.ashx' + (urlargs.key ? ('?key=' + urlargs.key) : '');
  23. if (obj.authCookie && (obj.authCookie != '')) { url += '?moreargs=1' }
  24. obj.socket = new WebSocket(url);
  25. obj.socket.onopen = function (e) { obj.connectstate = 1; if (obj.authCookie && (obj.authCookie != '')) { obj.send({ 'action': 'urlargs', 'args': { 'auth': obj.authCookie } }); } }
  26. obj.socket.onmessage = obj.xxOnMessage;
  27. obj.socket.onclose = function(e) { obj.Stop(e.code); }
  28. obj.xxStateChange(1, 0);
  29. if (obj.pingTimer != null) { clearInterval(obj.pingTimer); }
  30. obj.pingTimer = setInterval(function () { obj.send({ action: 'ping' }); }, 29000); // Ping the server every 29 seconds, stops corporate proxies from disconnecting.
  31. }
  32. obj.Stop = function (errCode) {
  33. obj.connectstate = 0;
  34. if (obj.socket) { obj.socket.close(); delete obj.socket; }
  35. if (obj.pingTimer != null) { clearInterval(obj.pingTimer); obj.pingTimer = null; }
  36. obj.xxStateChange(0, errCode);
  37. }
  38. obj.xxOnMessage = function (e) {
  39. if (obj.State == 1) { obj.xxStateChange(2); }
  40. //console.log('xxOnMessage', e.data);
  41. var message;
  42. try { message = JSON.parse(e.data); } catch (e) { return; }
  43. if ((typeof message != 'object') || (message.action == 'pong')) { return; }
  44. if (message.action == 'ping') { obj.send({ action: 'pong' }); }
  45. if (message.action == 'close') { if (message.msg) { console.log(message.msg); } obj.Stop(message.cause); return; }
  46. if (obj.trace == 1) { console.log('RECV', message); }
  47. else if (obj.trace == 2) { console.log('RECV', JSON.stringify(message)); }
  48. if (obj.onMessage) obj.onMessage(obj, message);
  49. };
  50. obj.send = function (x) {
  51. if (obj.socket != null && obj.connectstate == 1) {
  52. if (x.action != 'ping') {
  53. if (obj.trace == 1) { console.log('SEND', x); }
  54. else if (obj.trace == 2) { console.log('SEND', JSON.stringify(x)); }
  55. }
  56. obj.socket.send(JSON.stringify(x));
  57. }
  58. }
  59. return obj;
  60. }