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
|
import {CacheController, CacheStatus} from "./cache";
import * as io from "socket.io-client";
export class SocketController {
private static id = 1;
private _socket: SocketIOClient.Socket;
private _cacheController: CacheController;
// Mapping from request IDs to their registered callbacks
private callbacks: { [keys: number]: (response: IResponse) => any };
constructor(onConnect: () => any) {
this.callbacks = {};
this._cacheController = new CacheController();
this._socket = io.connect('https://opendc.ewi.tudelft.nl:443');
this._socket.on('connect', onConnect);
this._socket.on('response', (jsonResponse: string) => {
const response: IResponse = JSON.parse(jsonResponse);
console.log("Response, ID:", response.id, response);
this.callbacks[response.id](response);
delete this.callbacks[response.id];
});
}
/**
* Sends a request to the server socket and registers the callback to be triggered on response.
*
* @param request The request instance to be sent
* @param callback A function to be called with the response object once the socket has received a response
*/
public sendRequest(request: IRequest, callback: (response: IResponse) => any): void {
// Check local cache, in case request is for cachable GET route
const cacheStatus = this._cacheController.checkCache(request);
if (cacheStatus === CacheStatus.HIT) {
callback({
status: {
code: 200
},
content: this._cacheController.fetchFromCache(request),
id: -1
});
} else if (cacheStatus === CacheStatus.FETCHING) {
this._cacheController.registerCallback(request, callback);
} else if (cacheStatus === CacheStatus.MISS || cacheStatus === CacheStatus.NOT_CACHABLE) {
if (!this._socket.connected) {
console.error("Socket not connected, sending request failed");
}
if (cacheStatus === CacheStatus.MISS) {
this._cacheController.setToFetching(request);
this.callbacks[SocketController.id] = (response: IResponse) => {
this._cacheController.onFetch(request, response);
callback(response);
};
} else {
this.callbacks[SocketController.id] = callback;
}
// Setup request object
request.id = SocketController.id;
request.token = localStorage.getItem("googleToken");
request.path = "/v1" + request.path;
console.log("Request, ID:", request.id, request);
this._socket.emit("request", request);
SocketController.id++;
}
}
}
|