-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathws.js
91 lines (82 loc) · 3.09 KB
/
ws.js
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
'use strict';
// Provide a websocket implementation that behaves like $http
// Methods:
// $ws({
// url: "...", // required
// method: "...", // defaults to WATCH
// })
// returns a promise to the opened WebSocket
//
// $ws.available()
// returns true if WebSockets are available to use
angular.module('openshiftCommon')
.provider('$ws', function($httpProvider) {
// $get method is called to build the $ws service
this.$get = function($q, $injector, Logger) {
var authLogger = Logger.get("auth");
authLogger.log("$wsProvider.$get", arguments);
// Build list of interceptors from $httpProvider when constructing the $ws service
// Build in reverse-order, so the last interceptor added gets to handle the request first
var _interceptors = [];
angular.forEach($httpProvider.interceptors, function(interceptorFactory) {
if (angular.isString(interceptorFactory)) {
_interceptors.unshift($injector.get(interceptorFactory));
} else {
_interceptors.unshift($injector.invoke(interceptorFactory));
}
});
// Implement $ws()
var $ws = function(config) {
config.method = angular.uppercase(config.method || "WATCH");
authLogger.log("$ws (pre-intercept)", config.url.toString());
var serverRequest = function(config) {
authLogger.log("$ws (post-intercept)", config.url.toString());
var ws = new WebSocket(config.url, config.protocols);
if (config.onclose) { ws.onclose = config.onclose; }
if (config.onmessage) { ws.onmessage = config.onmessage; }
if (config.onopen) { ws.onopen = config.onopen; }
if (config.onerror) { ws.onerror = config.onerror; }
return ws;
};
// Apply interceptors to request config
var chain = [serverRequest, undefined];
var promise = $q.when(config);
angular.forEach(_interceptors, function(interceptor) {
if (interceptor.request || interceptor.requestError) {
chain.unshift(interceptor.request, interceptor.requestError);
}
// TODO: figure out how to get interceptors to handle response errors from web sockets
// if (interceptor.response || interceptor.responseError) {
// chain.push(interceptor.response, interceptor.responseError);
// }
});
while (chain.length) {
var thenFn = chain.shift();
var rejectFn = chain.shift();
promise = promise.then(thenFn, rejectFn);
}
return promise;
};
// Implement $ws.available()
$ws.available = function() {
try {
return !!WebSocket;
}
catch(e) {
return false;
}
};
return $ws;
};
})
/* A WebSocket factory for kubernetesContainerTerminal */
.factory("ContainerWebSocket", function(API_CFG, $ws) {
return function AuthWebSocket(url, protocols) {
var scheme;
if (url.indexOf("/") === 0) {
scheme = window.location.protocol === "http:" ? "ws://" : "wss://";
url = scheme + API_CFG.openshift.hostPort + url;
}
return $ws({ url: url, method: "WATCH", protocols: protocols, auth: {} });
};
});