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