@microsoft/windows-admin-center-sdk
Version:
Microsoft - Windows Admin Center Shell
150 lines (148 loc) • 4.88 kB
JavaScript
/// <reference types='signalr' />
import { HubConnectionBuilder, HubConnectionState } from '@microsoft/signalr';
/**
* Socket message flags. These can be combined.
*/
export var SocketMessageFlags;
(function (SocketMessageFlags) {
SocketMessageFlags[SocketMessageFlags["None"] = 0] = "None";
SocketMessageFlags[SocketMessageFlags["Data"] = 1] = "Data";
SocketMessageFlags[SocketMessageFlags["Progress"] = 2] = "Progress";
SocketMessageFlags[SocketMessageFlags["Completed"] = 4] = "Completed";
SocketMessageFlags[SocketMessageFlags["Error"] = 8] = "Error";
SocketMessageFlags[SocketMessageFlags["ConnectionError"] = 16] = "ConnectionError";
SocketMessageFlags[SocketMessageFlags["Exception"] = 32] = "Exception";
})(SocketMessageFlags || (SocketMessageFlags = {}));
/**
* SignalR based socket class.
*/
export class SocketSignalR {
gatewayUrl;
connectionUrl;
proxyName;
isGatewayV1;
connection;
connectionV1;
proxy;
started = false;
lastError;
processHandler = (message) => this.processMessage(message);
/**
* Gets signalR connection URL.
*/
get url() {
return `${this.gatewayUrl}${this.connectionUrl}`;
}
/**
* Instantiates a new instance of the SocketSignalR class.
*/
constructor(gatewayUrl, connectionUrl, proxyName, isGatewayV1) {
this.gatewayUrl = gatewayUrl;
this.connectionUrl = connectionUrl;
this.proxyName = proxyName;
this.isGatewayV1 = isGatewayV1;
gatewayUrl = gatewayUrl ? gatewayUrl : '';
}
/**
* Subscribe to the proxy with method name and handler.
*
* @param name the name of subscription for a method.
*/
subscribe(name) {
this.init();
if (this.lastError) {
// send last error;
this.clientHandler({ type: SocketMessageFlags.Error, connectionError: this.lastError, message: null });
}
if (this.isGatewayV1) {
return this.proxy.on(name, this.processHandler);
}
this.connection.on(name, this.processHandler);
}
/**
* Unsubscribe to the subscribed method call.
*
* @param name the name of subscription for a method.
*/
unsubscribe(name) {
if (this.isGatewayV1) {
this.proxy.off(name, this.processHandler);
return;
}
this.connection.off(name, this.processHandler);
}
/**
* Start request connection.
*/
start() {
if (this.isGatewayV1) {
return this.connectionV1.start().then(() => this.started = true);
}
return this.connection.start().then(() => this.started = true);
}
/**
* Stop request connection.
*/
stop() {
this.started = false;
if (this.isGatewayV1) {
this.connectionV1.stop();
}
this.connection.stop();
}
/**
* Invoke a method with parameters.
*
* @param name the method name to execute.
* @param args the parameters to pass.
* @return Promise the promise object.
*/
invoke(name, ...args) {
if (!this.started) {
throw new Error('request socket has not been started.');
}
if (this.isGatewayV1) {
if (this.connectionV1.state === 4 /* SignalR.ConnectionState.Disconnected: @types/signalr doesn't take conversion properly */) {
return this.start().then(() => this.proxy.invoke(name, ...args));
}
return this.proxy.invoke(name, ...args);
}
if (this.connection.state === HubConnectionState.Disconnected) {
return this.start().then(() => this.connection.invoke(name, ...args));
}
try {
return this.connection.invoke(name, ...args);
}
catch (error) {
this.errorMessage(error);
return Promise.reject(error);
}
}
/**
* Initiate the connection to gateway.
*/
init() {
if (this.isGatewayV1) {
this.connectionV1 = $.hubConnection(this.url);
this.connectionV1.error((error) => this.errorMessage(error));
this.proxy = this.connectionV1.createHubProxy(this.proxyName);
this.proxy.connection.error((error) => this.errorMessage(error));
return;
}
this.connection = new HubConnectionBuilder().withUrl(this.url).build();
}
/**
* Error message from signalr connection.
*
* @param error the error produced on the connection.
*/
errorMessage(error) {
if (this.clientHandler) {
this.clientHandler({ type: SocketMessageFlags.ConnectionError, connectionError: error, message: null });
}
else {
this.lastError = error;
}
}
}
//# sourceMappingURL=socket-signalr.js.map