who.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(["util", "channels", "session", "ui"], function (util, channels, session, ui) {
  5. var assert = util.assert;
  6. var who = util.Module("who");
  7. var MAX_RESPONSE_TIME = 5000;
  8. var MAX_LATE_RESPONSE = 2000;
  9. who.getList = function (hubUrl) {
  10. return util.Deferred(function (def) {
  11. var expected;
  12. var channel = channels.WebSocketChannel(hubUrl);
  13. var users = {};
  14. var responded = 0;
  15. var firstResponse = 0;
  16. var lateResponseTimeout;
  17. channel.onmessage = function (msg) {
  18. if (msg.type == "init-connection") {
  19. expected = msg.peerCount;
  20. }
  21. if (msg.type == "who") {
  22. // Our message back to ourselves probably
  23. firstResponse = setTimeout(function () {
  24. close();
  25. }, MAX_LATE_RESPONSE);
  26. }
  27. if (msg.type == "hello-back") {
  28. if (! users[msg.clientId]) {
  29. users[msg.clientId] = who.ExternalPeer(msg.clientId, msg);
  30. responded++;
  31. if (expected && responded >= expected) {
  32. close();
  33. } else {
  34. def.notify(users);
  35. }
  36. }
  37. }
  38. console.log("users", users);
  39. };
  40. channel.send({
  41. type: "who",
  42. "server-echo": true,
  43. clientId: null
  44. });
  45. var timeout = setTimeout(function () {
  46. close();
  47. }, MAX_RESPONSE_TIME);
  48. function close() {
  49. if (timeout) {
  50. clearTimeout(timeout);
  51. }
  52. if (lateResponseTimeout) {
  53. clearTimeout(lateResponseTimeout);
  54. }
  55. channel.close();
  56. def.resolve(users);
  57. }
  58. });
  59. };
  60. who.invite = function (hubUrl, clientId) {
  61. return util.Deferred(function (def) {
  62. var channel = channels.WebSocketChannel(hubUrl);
  63. var id = util.generateId();
  64. channel.onmessage = function (msg) {
  65. if (msg.type == "invite" && msg.inviteId == id) {
  66. channel.close();
  67. def.resolve();
  68. }
  69. };
  70. var userInfo = session.makeHelloMessage(false);
  71. delete userInfo.type;
  72. userInfo.clientId = session.clientId;
  73. channel.send({
  74. type: "invite",
  75. inviteId: id,
  76. url: session.shareUrl(),
  77. userInfo: userInfo,
  78. forClientId: clientId,
  79. clientId: null,
  80. "server-echo": true
  81. });
  82. });
  83. };
  84. who.ExternalPeer = util.Class({
  85. isSelf: false,
  86. isExternal: true,
  87. constructor: function (id, attrs) {
  88. attrs = attrs || {};
  89. assert(id);
  90. this.id = id;
  91. this.identityId = attrs.identityId || null;
  92. this.status = attrs.status || "live";
  93. this.idle = attrs.status || "active";
  94. this.name = attrs.name || null;
  95. this.avatar = attrs.avatar || null;
  96. this.color = attrs.color || "#00FF00";
  97. this.lastMessageDate = 0;
  98. this.view = ui.PeerView(this);
  99. },
  100. className: function (prefix) {
  101. prefix = prefix || "";
  102. return prefix + util.safeClassName(this.id);
  103. }
  104. });
  105. return who;
  106. });