| 177 | |
| 178 | |
| 179 | export class HttpSession implements Session { |
| 180 | interval_pull_id: number = null; |
| 181 | webio_session_id: string = ''; |
| 182 | debug = false; |
| 183 | |
| 184 | private sender: ReliableSender = null; |
| 185 | private _executed_command_msg_id = -1; |
| 186 | private _closed = false; |
| 187 | private _session_create_callbacks: (() => void)[] = []; |
| 188 | private _session_close_callbacks: (() => void)[] = []; |
| 189 | private _on_server_message: (msg: Command) => void = () => { |
| 190 | }; |
| 191 | |
| 192 | |
| 193 | constructor(public api_url: string, app_name = 'index', public pull_interval_ms = 1000) { |
| 194 | let url = new URL(api_url, window.location.href); |
| 195 | url.search = "?app=" + app_name; |
| 196 | this.api_url = url.href; |
| 197 | this.sender = new ReliableSender(this._send.bind(this)); |
| 198 | } |
| 199 | |
| 200 | on_session_create(callback: () => void): void { |
| 201 | this._session_create_callbacks.push(callback); |
| 202 | } |
| 203 | |
| 204 | on_session_close(callback: () => void): void { |
| 205 | this._session_close_callbacks.push(callback); |
| 206 | } |
| 207 | |
| 208 | on_server_message(callback: (msg: Command) => void): void { |
| 209 | this._on_server_message = callback; |
| 210 | } |
| 211 | |
| 212 | start_session(debug: boolean = false): void { |
| 213 | this.debug = debug; |
| 214 | this.webio_session_id = "NEW-" + randomid(24); |
| 215 | this.pull(); |
| 216 | this.interval_pull_id = setInterval(() => { |
| 217 | this.pull() |
| 218 | }, this.pull_interval_ms); |
| 219 | } |
| 220 | |
| 221 | pull() { |
| 222 | let that = this; |
| 223 | $.ajax({ |
| 224 | type: "GET", |
| 225 | url: `${this.api_url}&ack=${this._executed_command_msg_id}`, |
| 226 | contentType: "application/json; charset=utf-8", |
| 227 | dataType: "json", |
| 228 | headers: {"webio-session-id": this.webio_session_id}, |
| 229 | success: function (data: { commands: Command[][], seq: number, event: number, ack: number }, |
| 230 | textStatus: string, jqXHR: JQuery.jqXHR) { |
| 231 | safe_poprun_callbacks(that._session_create_callbacks, 'session_create_callback'); |
| 232 | that._on_request_success(data, textStatus, jqXHR); |
| 233 | if (that.webio_session_id.startsWith("NEW-")) { |
| 234 | that.webio_session_id = that.webio_session_id.substring(4); |
| 235 | } |
| 236 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…