recorder.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. define(["jquery", "util", "channels"], function ($, util, channels) {
  5. var recorder = util.Module("recorder");
  6. var assert = util.assert;
  7. var channel = null;
  8. var baseUrl = null;
  9. var clientId = "recorder";
  10. function display(el) {
  11. el = $(el);
  12. var toggles = el.attr("data-toggles");
  13. if (toggles) {
  14. $(toggles).hide();
  15. }
  16. el.show();
  17. }
  18. recorder.start = function (options) {
  19. $(function () {
  20. $("#record").css({height: $(window).height() - 50});
  21. $("#restart").click(function () {
  22. location.reload();
  23. });
  24. $("#select").click(function () {
  25. $("#record").select();
  26. });
  27. recorder.activate(options);
  28. });
  29. };
  30. recorder.activate = function (options) {
  31. var match;
  32. baseUrl = options.baseUrl;
  33. recorder.shareId = TogetherJS.startup._joinShareId;
  34. if (! recorder.shareId) {
  35. match = /\&togetherjs=([^&]+)/.exec(location.hash);
  36. if (! match) {
  37. display("#no-session-id");
  38. return;
  39. }
  40. recorder.shareId = match[1];
  41. }
  42. var hubBase = options.defaultHubBase;
  43. match = /\&hubBase=([^&]+)/.exec(location.hash);
  44. if (match) {
  45. hubBase = match[1];
  46. }
  47. hubBase = hubBase.replace(/\/*$/, "");
  48. var url = hubBase + "/hub/" + recorder.shareId;
  49. channel = channels.WebSocketChannel(url);
  50. channel.onmessage = function (msg) {
  51. if (msg.type == "hello-back") {
  52. display("#connected");
  53. }
  54. if (msg.type == "hello") {
  55. sendHello(true);
  56. }
  57. if (msg.type == "get-logs") {
  58. sendLogs(msg);
  59. return;
  60. }
  61. recorder.logMessage(msg);
  62. };
  63. sendHello(false);
  64. };
  65. function sendHello(helloBack) {
  66. var msg = {
  67. type: helloBack ? "hello-back" : "hello",
  68. name: "Recorder 'bot",
  69. // FIXME: replace with robot:
  70. avatar: TogetherJS.baseUrl + "/togetherjs/images/robot-avatar.png",
  71. color: "#888888",
  72. rtcSupported: false,
  73. clientId: clientId,
  74. url: "about:blank"
  75. };
  76. channel.send(msg);
  77. }
  78. function sendLogs(req) {
  79. var msg = {
  80. type: "logs",
  81. clientId: clientId,
  82. logs: $("#record").val(),
  83. request: req
  84. };
  85. channel.send(msg);
  86. }
  87. recorder.logMessage = function (msg) {
  88. msg.date = Date.now();
  89. msg = JSON.stringify(msg);
  90. var $record = $("#record");
  91. $record.val($record.val() + msg + "\n\n");
  92. };
  93. $(window).unload(function () {
  94. channel.send({
  95. type: "bye",
  96. clientId: clientId
  97. });
  98. });
  99. return recorder;
  100. });