Remote web console.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. class Conn {
  2. constructor(id) {
  3. this.id = id;
  4. this.conn = null;
  5. this.q = [];
  6. this.onstatechange = () => {}
  7. this.onmessage = () => {};
  8. this.createConn();
  9. setInterval(() => this.send({ type: "ping" }), 30000);
  10. }
  11. createConn() {
  12. this.conn = new WebSocket(`${location.protocol.replace("http", "ws")}//${location.host}`);
  13. this.onstatechange("Connecting");
  14. this.connected = false;
  15. this.ready = false;
  16. this.conn.onclose = evt => {
  17. this.conn = null;
  18. this.onstatechange(`Closed: ${evt.code}`);
  19. setTimeout(this.createConn.bind(this), 2000);
  20. }
  21. this.conn.onopen = () => {
  22. this.onstatechange("Identifying");
  23. this.conn.send(JSON.stringify({ type: "identify-master", id: this.id }));
  24. this.q.forEach(el => this.conn.send(el));
  25. this.q = [];
  26. }
  27. this.conn.onmessage = msg => {
  28. console.log(msg.data);
  29. let obj = JSON.parse(msg.data);
  30. switch (obj.type) {
  31. case "identify-response":
  32. if (obj.err == null) {
  33. this.connected = true;
  34. this.onstatechange("Waiting for target");
  35. } else {
  36. this.onstatechange(`Error: ${obj.err}`);
  37. }
  38. break;
  39. case "target-connect":
  40. this.ready = true;
  41. this.onstatechange("Ready");
  42. break;
  43. case "target-disconnect":
  44. this.ready = false;
  45. this.onstatechange("Waiting for target");
  46. break;
  47. case "js-result":
  48. this.onjsresult(obj.err, obj.result);
  49. break;
  50. case "log":
  51. this.onlog(obj.log);
  52. break;
  53. case "pong":
  54. break;
  55. default:
  56. console.warn("Unknown message type:", obj.type);
  57. }
  58. }
  59. }
  60. runJavascript(js) {
  61. this.send({ type: "run-js", js });
  62. }
  63. send(obj) {
  64. if (this.connected)
  65. this.conn.send(JSON.stringify(obj));
  66. }
  67. }