Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let WebSocketClient take in a disconnection callback #1291

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,19 @@ export class WebSocketClient implements IStreamingTransportClient {
private readonly _receiver: PayloadReceiver;
private readonly _requestManager: RequestManager;
private readonly _protocolAdapter: ProtocolAdapter;
private readonly _autoReconnect: boolean;
private readonly _disconnectionHandler: (message: string) => void;

/// <summary>
/// Initializes a new instance of the <see cref="WebSocketClient"/> class.
/// </summary>
/// <param name="url">The URL of the remote server to connect to.</param>
/// <param name="requestHandler">Optional <see cref="RequestHandler"/> to process incoming messages received by this server.</param>
/// <param name="autoReconnect">Optional setting to determine if the server sould attempt to reconnect
/// automatically on disconnection events. Defaults to true.
/// <param name="disconnectionHandler ">Optional function to handle the disconnection message</param>
/// </param>
public constructor({ url, requestHandler, autoReconnect = true }) {
public constructor({ url, requestHandler, disconnectionHandler = null}) {
this._url = url;
this._requestHandler = requestHandler;
this._autoReconnect = autoReconnect;
this._disconnectionHandler = disconnectionHandler;

this._requestManager = new RequestManager();

Expand Down Expand Up @@ -96,10 +95,11 @@ export class WebSocketClient implements IStreamingTransportClient {
}

private onConnectionDisconnected(sender: object, args: any): void {
if (this._autoReconnect) {
this.connect()
.catch((): void => { throw(new Error(`Unable to re-connect client to Node transport. Sender:` + sender + ' Args:' + args)); });
if (this._disconnectionHandler != null) {
this._disconnectionHandler("Disconnected");
return;
}
}

throw(new Error(`Unable to re-connect client to Node transport. Sender:` + sender + ' Args:' + args));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ describe('Streaming Extensions WebSocket Library Tests', () => {

describe('WebSocket Client Tests', () => {
it('creates a new client', () => {
let client = new ws.WebSocketClient('fakeURL', new protocol.RequestHandler(), false);
let client = new ws.WebSocketClient('fakeURL', new protocol.RequestHandler());
expect(client).to.be.instanceOf(ws.WebSocketClient);
expect(client.disconnect()).to.not.throw;
});

it('selects the right websocket and attempts to connect to the transport layer', (done) => {
let client = new ws.WebSocketClient('fakeURL', new protocol.RequestHandler(), false);
let client = new ws.WebSocketClient('fakeURL', new protocol.RequestHandler());
expect(client).to.be.instanceOf(ws.WebSocketClient);
client.connect()
.catch(
Expand All @@ -261,7 +261,7 @@ describe('Streaming Extensions WebSocket Library Tests', () => {
});

it('sends', (done) => {
let client = new ws.WebSocketClient('fakeURL', new protocol.RequestHandler(), false);
let client = new ws.WebSocketClient('fakeURL', new protocol.RequestHandler());
expect(client).to.be.instanceOf(ws.WebSocketClient);
let req = new protocol.StreamingRequest();
req.Verb = 'POST';
Expand All @@ -271,7 +271,7 @@ describe('Streaming Extensions WebSocket Library Tests', () => {
});

it('disconnects', (done) => {
let client = new ws.WebSocketClient('fakeURL', new protocol.RequestHandler(), false);
let client = new ws.WebSocketClient('fakeURL', new protocol.RequestHandler());
expect(client).to.be.instanceOf(ws.WebSocketClient);
expect(client.disconnect()).to.not.throw;
done();
Expand Down