This repository has been archived by the owner on Dec 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 445
/
Transports.ts
281 lines (235 loc) · 8.96 KB
/
Transports.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
import { DataReceived, TransportClosed } from "./Common"
import { IHttpClient } from "./HttpClient"
import { HttpError } from "./HttpError"
import { ILogger, LogLevel } from "./ILogger"
export enum TransportType {
WebSockets,
ServerSentEvents,
LongPolling
}
export const enum TransferMode {
Text = 1,
Binary
}
export interface ITransport {
connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode>;
send(data: any): Promise<void>;
stop(): void;
onDataReceived: DataReceived;
onClosed: TransportClosed;
}
export class WebSocketTransport implements ITransport {
private readonly logger: ILogger;
private webSocket: WebSocket;
constructor(logger: ILogger) {
this.logger = logger;
}
connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode> {
return new Promise<TransferMode>((resolve, reject) => {
url = url.replace(/^http/, "ws");
let webSocket = new WebSocket(url);
if (requestedTransferMode == TransferMode.Binary) {
webSocket.binaryType = "arraybuffer";
}
webSocket.onopen = (event: Event) => {
this.logger.log(LogLevel.Information, `WebSocket connected to ${url}`);
this.webSocket = webSocket;
resolve(requestedTransferMode);
};
webSocket.onerror = (event: Event) => {
reject();
};
webSocket.onmessage = (message: MessageEvent) => {
this.logger.log(LogLevel.Trace, `(WebSockets transport) data received: ${message.data}`);
if (this.onDataReceived) {
this.onDataReceived(message.data);
}
}
webSocket.onclose = (event: CloseEvent) => {
// webSocket will be null if the transport did not start successfully
if (this.onClosed && this.webSocket) {
if (event.wasClean === false || event.code !== 1000) {
this.onClosed(new Error(`Websocket closed with status code: ${event.code} (${event.reason})`));
}
else {
this.onClosed();
}
}
}
});
}
send(data: any): Promise<void> {
if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) {
this.webSocket.send(data);
return Promise.resolve();
}
return Promise.reject("WebSocket is not in the OPEN state");
}
stop(): void {
if (this.webSocket) {
this.webSocket.close();
this.webSocket = null;
}
}
onDataReceived: DataReceived;
onClosed: TransportClosed;
}
export class ServerSentEventsTransport implements ITransport {
private readonly httpClient: IHttpClient;
private readonly logger: ILogger;
private eventSource: EventSource;
private url: string;
constructor(httpClient: IHttpClient, logger: ILogger) {
this.httpClient = httpClient;
this.logger = logger;
}
connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode> {
if (typeof (EventSource) === "undefined") {
Promise.reject("EventSource not supported by the browser.");
}
this.url = url;
return new Promise<TransferMode>((resolve, reject) => {
let eventSource = new EventSource(this.url);
try {
eventSource.onmessage = (e: MessageEvent) => {
if (this.onDataReceived) {
try {
this.logger.log(LogLevel.Trace, `(SSE transport) data received: ${e.data}`);
this.onDataReceived(e.data);
} catch (error) {
if (this.onClosed) {
this.onClosed(error);
}
return;
}
}
};
eventSource.onerror = (e: ErrorEvent) => {
reject();
// don't report an error if the transport did not start successfully
if (this.eventSource && this.onClosed) {
this.onClosed(new Error(e.message || "Error occurred"));
}
}
eventSource.onopen = () => {
this.logger.log(LogLevel.Information, `SSE connected to ${this.url}`);
this.eventSource = eventSource;
// SSE is a text protocol
resolve(TransferMode.Text);
}
}
catch (e) {
return Promise.reject(e);
}
});
}
async send(data: any): Promise<void> {
return send(this.httpClient, this.url, data);
}
stop(): void {
if (this.eventSource) {
this.eventSource.close();
this.eventSource = null;
}
}
onDataReceived: DataReceived;
onClosed: TransportClosed;
}
export class LongPollingTransport implements ITransport {
private readonly httpClient: IHttpClient;
private readonly logger: ILogger;
private url: string;
private pollXhr: XMLHttpRequest;
private shouldPoll: boolean;
constructor(httpClient: IHttpClient, logger: ILogger) {
this.httpClient = httpClient;
this.logger = logger;
}
connect(url: string, requestedTransferMode: TransferMode): Promise<TransferMode> {
this.url = url;
this.shouldPoll = true;
if (requestedTransferMode === TransferMode.Binary && (typeof new XMLHttpRequest().responseType !== "string")) {
// This will work if we fix: https://github.com/aspnet/SignalR/issues/742
throw new Error("Binary protocols over XmlHttpRequest not implementing advanced features are not supported.");
}
this.poll(this.url, requestedTransferMode);
return Promise.resolve(requestedTransferMode);
}
private poll(url: string, transferMode: TransferMode): void {
if (!this.shouldPoll) {
return;
}
let pollXhr = new XMLHttpRequest();
pollXhr.onload = () => {
if (pollXhr.status == 200) {
if (this.onDataReceived) {
try {
let response = transferMode === TransferMode.Text
? pollXhr.responseText
: pollXhr.response;
if (response) {
this.logger.log(LogLevel.Trace, `(LongPolling transport) data received: ${response}`);
this.onDataReceived(response);
}
else {
this.logger.log(LogLevel.Information, "(LongPolling transport) timed out");
}
} catch (error) {
if (this.onClosed) {
this.onClosed(error);
}
return;
}
}
this.poll(url, transferMode);
}
else if (this.pollXhr.status == 204) {
if (this.onClosed) {
this.onClosed();
}
}
else {
if (this.onClosed) {
this.onClosed(new HttpError(pollXhr.statusText, pollXhr.status));
}
}
};
pollXhr.onerror = () => {
if (this.onClosed) {
// network related error or denied cross domain request
this.onClosed(new Error("Sending HTTP request failed."));
}
};
pollXhr.ontimeout = () => {
this.poll(url, transferMode);
}
this.pollXhr = pollXhr;
this.pollXhr.open("GET", url, true);
if (transferMode === TransferMode.Binary) {
this.pollXhr.responseType = "arraybuffer";
}
// IE caches xhr requests
this.pollXhr.setRequestHeader("Cache-Control", "no-cache");
// TODO: consider making timeout configurable
this.pollXhr.timeout = 120000;
this.pollXhr.send();
}
async send(data: any): Promise<void> {
return send(this.httpClient, this.url, data);
}
stop(): void {
this.shouldPoll = false;
if (this.pollXhr) {
this.pollXhr.abort();
this.pollXhr = null;
}
}
onDataReceived: DataReceived;
onClosed: TransportClosed;
}
const headers = new Map<string, string>();
async function send(httpClient: IHttpClient, url: string, data: any): Promise<void> {
await httpClient.post(url, data, headers);
}