server.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. class Server {
  2. constructor(apiDataUrl, websocketUrl) {
  3. this.socketHandlers = [];
  4. this.apidataurl = apiDataUrl;
  5. this.websocketurl = websocketUrl;
  6. this.webSocket = null;
  7. }
  8. apiGet = (path) => {
  9. return new Promise((resolve, reject) => {
  10. var r = new XMLHttpRequest();
  11. r.open("GET", this.apidataurl + path, true);
  12. r.onreadystatechange = function (response) {
  13. if (r.readyState == 4 && r.status == 200) {
  14. resolve(JSON.parse(r.responseText));
  15. }
  16. if (r.readyState == 4 && r.status != 200) {
  17. reject(JSON.parse(r.response));
  18. }
  19. return;
  20. }
  21. r.onerror = function (response) {
  22. console.error("onerror", response, r);
  23. reject(r.responseText);
  24. }
  25. r.setRequestHeader("Content-Type", "application/json");
  26. r.send();
  27. });
  28. };
  29. apiPost = (path, data) => {
  30. return new Promise((resolve, reject) => {
  31. var r = new XMLHttpRequest();
  32. r.open("POST", this.apidataurl + path, true);
  33. r.onreadystatechange = function (response) {
  34. if (r.readyState == 4 && r.status == 200) {
  35. resolve(JSON.parse(r.responseText));
  36. }
  37. if (r.readyState == 4 && r.status != 200) {
  38. reject(r.responseText);
  39. }
  40. return;
  41. }
  42. r.onerror = function (response) {
  43. console.error("onerror", response, r);
  44. reject(r.responseText);
  45. }
  46. r.setRequestHeader("Content-Type", "application/json");
  47. let jsonData = JSON.stringify(data);
  48. r.send(jsonData);
  49. });
  50. };
  51. websocketConnect = () => {
  52. this.webSocket = new WebSocket(this.websocketurl);
  53. this.webSocket.onmessage = this.handleWebsocketMessage;
  54. this.webSocket.onopen = this.handleWebsocketOpen;
  55. this.webSocket.onclose = this.handleWebsocketClose;
  56. this.webSocket.onerror = this.handleWebsocketError;
  57. };
  58. websocketSend = (message) => {
  59. try {
  60. this.ebSocket.send(message);
  61. } catch (error) {
  62. console.error("websocket", error);
  63. }
  64. };
  65. attachWebsocketHandler = (handler) => {
  66. this.socketHandlers.push(handler);
  67. }
  68. removeWebsocketHandler = (handler) => {
  69. this.socketHandlers.splice(this.socketHandlers.indexOf(handler), 1);
  70. }
  71. handleWebsocketMessage = (event) => {
  72. this.socketHandlers.forEach(handler => {
  73. if(handler.hasOwnProperty("onmessage")){
  74. handler.onmessage(event.data);
  75. }
  76. })
  77. }
  78. handleWebsocketOpen = (event) => {
  79. this.socketHandlers.forEach(handler => {
  80. if(handler.hasOwnProperty("onopen")){
  81. handler.onopen(event);
  82. }
  83. })
  84. }
  85. handleWebsocketClose = (event) => {
  86. this.socketHandlers.forEach(handler => {
  87. if(handler.hasOwnProperty("onclose")){
  88. handler.onclose(event);
  89. }
  90. })
  91. }
  92. handleWebsocketError = (event) => {
  93. this.socketHandlers.forEach(handler => {
  94. if(handler.hasOwnProperty("onerror")){
  95. handler.onerror(event);
  96. }
  97. })
  98. }
  99. }