| 100 | var readyForMessages = false; |
| 101 | |
| 102 | function openChannel() { |
| 103 | assert(! channel, "Attempt to re-open channel"); |
| 104 | console.info("Connecting to", session.hubUrl(), location.href); |
| 105 | var c = channels.WebSocketChannel(session.hubUrl()); |
| 106 | c.onmessage = function (msg) { |
| 107 | if (! readyForMessages) { |
| 108 | if (DEBUG) { |
| 109 | console.info("In (but ignored for being early):", msg); |
| 110 | } |
| 111 | return; |
| 112 | } |
| 113 | if (DEBUG && IGNORE_MESSAGES.indexOf(msg.type) == -1) { |
| 114 | console.info("In:", msg); |
| 115 | } |
| 116 | if (! peers) { |
| 117 | // We're getting messages before everything is fully initialized |
| 118 | console.warn("Message received before all modules loaded (ignoring):", msg); |
| 119 | return; |
| 120 | } |
| 121 | if ((! msg.clientId) && MESSAGES_WITHOUT_CLIENTID.indexOf(msg.type) == -1) { |
| 122 | console.warn("Got message without clientId, where clientId is required", msg); |
| 123 | return; |
| 124 | } |
| 125 | if (msg.clientId) { |
| 126 | msg.peer = peers.getPeer(msg.clientId, msg); |
| 127 | } |
| 128 | if (msg.type == "hello" || msg.type == "hello-back" || msg.type == "peer-update") { |
| 129 | // We do this here to make sure this is run before any other |
| 130 | // hello handlers: |
| 131 | msg.peer.updateFromHello(msg); |
| 132 | } |
| 133 | if (msg.peer) { |
| 134 | msg.sameUrl = msg.peer.url == currentUrl; |
| 135 | if (!msg.peer.isSelf) { |
| 136 | msg.peer.updateMessageDate(msg); |
| 137 | } |
| 138 | } |
| 139 | session.hub.emit(msg.type, msg); |
| 140 | TogetherJS._onmessage(msg); |
| 141 | }; |
| 142 | channel = c; |
| 143 | session.router.bindChannel(channel); |
| 144 | } |
| 145 | |
| 146 | session.send = function (msg) { |
| 147 | if (DEBUG && IGNORE_MESSAGES.indexOf(msg.type) == -1) { |