Show More
@@ -1,219 +1,219 b'' | |||
|
1 | 1 | "use strict"; |
|
2 | 2 | /** leak object to top level scope **/ |
|
3 | 3 | var ccLog = undefined; |
|
4 | 4 | // global code-mirror logger;, to enable run |
|
5 | 5 | // Logger.get('ConnectionController').setLevel(Logger.DEBUG) |
|
6 | 6 | ccLog = Logger.get('ConnectionController'); |
|
7 | 7 | ccLog.setLevel(Logger.OFF); |
|
8 | 8 | |
|
9 | 9 | var ConnectionController; |
|
10 | 10 | var connCtrlr; |
|
11 | 11 | var registerViewChannels; |
|
12 | 12 | |
|
13 | 13 | (function () { |
|
14 | 14 | ConnectionController = function (webappUrl, serverUrl, urls) { |
|
15 | 15 | var self = this; |
|
16 | 16 | |
|
17 | 17 | var channels = ['broadcast']; |
|
18 | 18 | this.state = { |
|
19 | 19 | open: false, |
|
20 | 20 | webappUrl: webappUrl, |
|
21 | 21 | serverUrl: serverUrl, |
|
22 | 22 | connId: null, |
|
23 | 23 | socket: null, |
|
24 | 24 | channels: channels, |
|
25 | 25 | heartbeat: null, |
|
26 | 26 | channelsInfo: {}, |
|
27 | 27 | urls: urls |
|
28 | 28 | }; |
|
29 | 29 | this.channelNameParsers = []; |
|
30 | 30 | |
|
31 | 31 | this.addChannelNameParser = function (fn) { |
|
32 | 32 | if (this.channelNameParsers.indexOf(fn) === -1) { |
|
33 | 33 | this.channelNameParsers.push(fn); |
|
34 | 34 | } |
|
35 | 35 | }; |
|
36 | 36 | |
|
37 | 37 | this.listen = function () { |
|
38 | 38 | if (window.WebSocket) { |
|
39 | 39 | ccLog.debug('attempting to create socket'); |
|
40 | 40 | var socket_url = self.state.serverUrl + "/ws?conn_id=" + self.state.connId; |
|
41 | 41 | var socket_conf = { |
|
42 | 42 | url: socket_url, |
|
43 | 43 | handleAs: 'json', |
|
44 | 44 | headers: { |
|
45 | 45 | "Accept": "application/json", |
|
46 | 46 | "Content-Type": "application/json" |
|
47 | 47 | } |
|
48 | 48 | }; |
|
49 | 49 | self.state.socket = new WebSocket(socket_conf.url); |
|
50 | 50 | |
|
51 | 51 | self.state.socket.onopen = function (event) { |
|
52 | 52 | ccLog.debug('open event', event); |
|
53 | 53 | if (self.state.heartbeat === null) { |
|
54 | 54 | self.state.heartbeat = setInterval(function () { |
|
55 | 55 | if (self.state.socket.readyState === WebSocket.OPEN) { |
|
56 | 56 | self.state.socket.send('heartbeat'); |
|
57 | 57 | } |
|
58 | 58 | }, 10000) |
|
59 | 59 | } |
|
60 | 60 | }; |
|
61 | 61 | self.state.socket.onmessage = function (event) { |
|
62 | 62 | var data = $.parseJSON(event.data); |
|
63 | 63 | for (var i = 0; i < data.length; i++) { |
|
64 | 64 | if (data[i].message.topic) { |
|
65 | 65 | ccLog.debug('publishing', |
|
66 | 66 | data[i].message.topic, data[i]); |
|
67 | 67 | $.Topic(data[i].message.topic).publish(data[i]) |
|
68 | 68 | } |
|
69 | 69 | else { |
|
70 |
cc |
|
|
70 | ccLog.warning('unhandled message', data); | |
|
71 | 71 | } |
|
72 | 72 | } |
|
73 | 73 | }; |
|
74 | 74 | self.state.socket.onclose = function (event) { |
|
75 | 75 | ccLog.debug('closed event', event); |
|
76 | 76 | setTimeout(function () { |
|
77 | 77 | self.connect(true); |
|
78 | 78 | }, 5000); |
|
79 | 79 | }; |
|
80 | 80 | |
|
81 | 81 | self.state.socket.onerror = function (event) { |
|
82 | 82 | ccLog.debug('error event', event); |
|
83 | 83 | }; |
|
84 | 84 | } |
|
85 | 85 | else { |
|
86 | 86 | ccLog.debug('attempting to create long polling connection'); |
|
87 | 87 | var poolUrl = self.state.serverUrl + "/listen?conn_id=" + self.state.connId; |
|
88 | 88 | self.state.socket = $.ajax({ |
|
89 | 89 | url: poolUrl |
|
90 | 90 | }).done(function (data) { |
|
91 | 91 | ccLog.debug('data', data); |
|
92 | 92 | var data = $.parseJSON(data); |
|
93 | 93 | for (var i = 0; i < data.length; i++) { |
|
94 | 94 | if (data[i].message.topic) { |
|
95 | 95 | ccLog.info('publishing', |
|
96 | 96 | data[i].message.topic, data[i]); |
|
97 | 97 | $.Topic(data[i].message.topic).publish(data[i]) |
|
98 | 98 | } |
|
99 | 99 | else { |
|
100 |
cc |
|
|
100 | ccLog.warning('unhandled message', data); | |
|
101 | 101 | } |
|
102 | 102 | } |
|
103 | 103 | self.listen(); |
|
104 | 104 | }).fail(function () { |
|
105 | 105 | ccLog.debug('longpoll error'); |
|
106 | 106 | setTimeout(function () { |
|
107 | 107 | self.connect(true); |
|
108 | 108 | }, 5000); |
|
109 | 109 | }); |
|
110 | 110 | } |
|
111 | 111 | |
|
112 | 112 | }; |
|
113 | 113 | |
|
114 | 114 | this.connect = function (create_new_socket) { |
|
115 | 115 | var connReq = {'channels': self.state.channels}; |
|
116 | 116 | ccLog.debug('try obtaining connection info', connReq); |
|
117 | 117 | $.ajax({ |
|
118 | 118 | url: self.state.urls.connect, |
|
119 | 119 | type: "POST", |
|
120 | 120 | contentType: "application/json", |
|
121 | 121 | data: JSON.stringify(connReq), |
|
122 | 122 | dataType: "json" |
|
123 | 123 | }).done(function (data) { |
|
124 | 124 | ccLog.debug('Got connection:', data.conn_id); |
|
125 | 125 | self.state.channels = data.channels; |
|
126 | 126 | self.state.channelsInfo = data.channels_info; |
|
127 | 127 | self.state.connId = data.conn_id; |
|
128 | 128 | if (create_new_socket) { |
|
129 | 129 | self.listen(); |
|
130 | 130 | } |
|
131 | 131 | self.update(); |
|
132 | 132 | }).fail(function () { |
|
133 | 133 | setTimeout(function () { |
|
134 | 134 | self.connect(create_new_socket); |
|
135 | 135 | }, 5000); |
|
136 | 136 | }); |
|
137 | 137 | self.update(); |
|
138 | 138 | }; |
|
139 | 139 | |
|
140 | 140 | this.subscribeToChannels = function (channels) { |
|
141 | 141 | var new_channels = []; |
|
142 | 142 | for (var i = 0; i < channels.length; i++) { |
|
143 | 143 | var channel = channels[i]; |
|
144 | 144 | if (self.state.channels.indexOf(channel)) { |
|
145 | 145 | self.state.channels.push(channel); |
|
146 | 146 | new_channels.push(channel) |
|
147 | 147 | } |
|
148 | 148 | } |
|
149 | 149 | /** |
|
150 | 150 | * only execute the request if socket is present because subscribe |
|
151 | 151 | * can actually add channels before initial app connection |
|
152 | 152 | **/ |
|
153 | 153 | if (new_channels && self.state.socket !== null) { |
|
154 | 154 | var connReq = { |
|
155 | 155 | 'channels': self.state.channels, |
|
156 | 156 | 'conn_id': self.state.connId |
|
157 | 157 | }; |
|
158 | 158 | $.ajax({ |
|
159 | 159 | url: self.state.urls.subscribe, |
|
160 | 160 | type: "POST", |
|
161 | 161 | contentType: "application/json", |
|
162 | 162 | data: JSON.stringify(connReq), |
|
163 | 163 | dataType: "json" |
|
164 | 164 | }).done(function (data) { |
|
165 | 165 | self.state.channels = data.channels; |
|
166 | 166 | self.state.channelsInfo = data.channels_info; |
|
167 | 167 | self.update(); |
|
168 | 168 | }); |
|
169 | 169 | } |
|
170 | 170 | self.update(); |
|
171 | 171 | }; |
|
172 | 172 | |
|
173 | 173 | this.update = function () { |
|
174 | 174 | for (var key in this.state.channelsInfo) { |
|
175 | 175 | if (this.state.channelsInfo.hasOwnProperty(key)) { |
|
176 | 176 | // update channels with latest info |
|
177 | 177 | $.Topic('/connection_controller/channel_update').publish( |
|
178 | 178 | {channel: key, state: this.state.channelsInfo[key]}); |
|
179 | 179 | } |
|
180 | 180 | } |
|
181 | 181 | /** |
|
182 | 182 | * checks current channel list in state and if channel is not present |
|
183 | 183 | * converts them into executable "commands" and pushes them on topics |
|
184 | 184 | */ |
|
185 | 185 | for (var i = 0; i < this.state.channels.length; i++) { |
|
186 | 186 | var channel = this.state.channels[i]; |
|
187 | 187 | for (var j = 0; j < this.channelNameParsers.length; j++) { |
|
188 | 188 | this.channelNameParsers[j](channel); |
|
189 | 189 | } |
|
190 | 190 | } |
|
191 | 191 | }; |
|
192 | 192 | |
|
193 | 193 | this.run = function () { |
|
194 | 194 | this.connect(true); |
|
195 | 195 | }; |
|
196 | 196 | |
|
197 | 197 | $.Topic('/connection_controller/subscribe').subscribe( |
|
198 | 198 | self.subscribeToChannels); |
|
199 | 199 | }; |
|
200 | 200 | |
|
201 | 201 | $.Topic('/plugins/__REGISTER__').subscribe(function (data) { |
|
202 | 202 | // enable chat controller |
|
203 | 203 | if (window.CHANNELSTREAM_SETTINGS && window.CHANNELSTREAM_SETTINGS.enabled) { |
|
204 | 204 | $(document).ready(function () { |
|
205 | 205 | connCtrlr.run(); |
|
206 | 206 | }); |
|
207 | 207 | } |
|
208 | 208 | }); |
|
209 | 209 | |
|
210 | 210 | registerViewChannels = function (){ |
|
211 | 211 | // subscribe to PR repo channel for PR's' |
|
212 | 212 | if (templateContext.pull_request_data.pull_request_id) { |
|
213 | 213 | var channelName = '/repo$' + templateContext.repo_name + '$/pr/' + |
|
214 | 214 | String(templateContext.pull_request_data.pull_request_id); |
|
215 | 215 | connCtrlr.state.channels.push(channelName); |
|
216 | 216 | } |
|
217 | 217 | } |
|
218 | 218 | |
|
219 | 219 | })(); |
General Comments 0
You need to be logged in to leave comments.
Login now