proxy.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // Copyright 2012 Square Inc.
  3. // Portions Copyright (c) 2016-present, Facebook, Inc.
  4. // All rights reserved.
  5. //
  6. // This source code is licensed under the license found in the
  7. // LICENSE-examples file in the root directory of this source tree.
  8. //
  9. function SocketClient () {
  10. this.list_elem = document.getElementById('client_list');
  11. this.info_div = document.getElementById('info_div');
  12. var self = this;
  13. }
  14. SocketClient.prototype.connect = function () {
  15. var self = this;
  16. this.list_elem.innerHTML = '';
  17. this.info_div.innerHTML = 'status: connecting...';
  18. this.socket = new WebSocket("ws://" + document.location.host + "/chat");
  19. this.socket.onopen = function () {self.onopen.apply(self, arguments);};
  20. this.socket.onmessage = function () {self.onmessage.apply(self, arguments);};
  21. this.socket.onclose = function () {self.onclose.apply(self, arguments);};
  22. };
  23. SocketClient.prototype.deviceAdded = function (params) {
  24. var el = document.createElement('li');
  25. el.innerHTML = '<a href="devtools/devtools.html?host=' + document.location.host + '&page=' + params.page + '">' + params.device_name + '</a>' + ' <span class="device_details">(' + params.app_id + ', ' + params.device_model + ', ' + params.device_id + ')</span>';
  26. this.list_elem.appendChild(el);
  27. this.visibleElems[params.connection_id] = el;
  28. };
  29. SocketClient.prototype.deviceRemoved = function (params) {
  30. var li = this.visibleElems[params.connection_id];
  31. li.parentNode.removeChild(li);
  32. };
  33. SocketClient.prototype.onopen = function () {
  34. this.info_div.innerHTML = 'status: connected to gateway';
  35. this.list_elem.innerHTML = '';
  36. this.visibleElems = {};
  37. console.log('connection to gateway opened');
  38. };
  39. SocketClient.prototype.onmessage = function (message) {
  40. var el = document.createElement('li');
  41. el.innerHTML = message.data;
  42. window.document.getElementById('history').appendChild(el);
  43. };
  44. SocketClient.prototype.onclose = function () {
  45. var retryInterval = 1000.0;
  46. console.log('connection closed, retrying in ' + (retryInterval/1000.0) + ' seconds');
  47. var self = this;
  48. window.setTimeout(function () {self.connect();}, retryInterval);
  49. };
  50. window.addEventListener('load', function () {
  51. var form = window.document.getElementById('msg_form');
  52. var msg_field = window.document.getElementById('msg_field');
  53. var socketClient = new SocketClient();
  54. socketClient.connect()
  55. form.onsubmit = function () {
  56. msg = msg_field.value;
  57. msg_field.value = '';
  58. socketClient.socket.send(msg);
  59. var el = document.createElement('li');
  60. el.innerHTML = msg;
  61. window.document.getElementById('history').appendChild(el);
  62. return false;
  63. };
  64. }
  65. );