Skip to content

Commit

Permalink
Allow websockets usage without creating container (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a authored and AlexGhiondea committed Mar 18, 2019
1 parent f00d0af commit fc54de2
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions lib/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Container } from "./container";
import { defaultOperationTimeoutInSeconds } from "./util/constants";
import { Func, EmitParameters, emitEvent } from "./util/utils";
import {
ConnectionEvents, SessionEvents, SenderEvents, ReceiverEvents, create_connection,
ConnectionEvents, SessionEvents, SenderEvents, ReceiverEvents, create_connection, websocket_connect,
ConnectionOptions as RheaConnectionOptions, Connection as RheaConnection, AmqpError, Dictionary,
ConnectionError, EventContext as RheaEventContext
} from "rhea";
Expand Down Expand Up @@ -49,8 +49,40 @@ export interface ConnectionOptions extends RheaConnectionOptions {
* timeout occurs. Default: `60 seconds`.
*/
operationTimeoutInSeconds?: number;

/**
* @property {Object} [webSocketOptions] - Options that include a web socket constructor along
* with arguments to be passed to the function returned by rhea.websocket_connect()
* This is required when the connection needs to use web sockets but is being created without
* creating a container first. If a container is already available, use `websocket_connect` on it
* directly instead.
*/
webSocketOptions?: {
/**
* @property {any} [webSocket] - The WebSocket constructor used to create an AMQP
* connection over a WebSocket.
*/
webSocket: any;
/**
* @property {string} [url] - Websocket url which will be passed to the function returned by
* rhea.websocket_connect()
*/
url: string;
/**
* @property {string[]} {protocol} - Websocket SubProtocol to be passed to the function
* returned by rhea.websocket_connect()
*/
protocol: string[],
/***
* @property {any} {options} - Options to be passed to the function returned by
* rhea.websocket_connect()
*/
options?: any
};
}



/**
* Describes the options that can be provided while creating a rhea-promise connection from an
* already created rhea connection object.
Expand Down Expand Up @@ -145,7 +177,15 @@ export class Connection extends Entity {
this._connection = (options as CreatedRheaConnectionOptions).rheaConnection;
this.container = (options as CreatedRheaConnectionOptions).container;
} else {
this._connection = create_connection(options as ConnectionOptions);
const connectionOptions = options as ConnectionOptions;
if (connectionOptions.webSocketOptions) {
const ws = websocket_connect(connectionOptions.webSocketOptions.webSocket);
(connectionOptions.connection_details as any) = ws(
connectionOptions.webSocketOptions.url,
connectionOptions.webSocketOptions.protocol,
connectionOptions.webSocketOptions.options);
}
this._connection = create_connection(connectionOptions);
this.container = Container.copyFromContainerInstance(this._connection.container);
}

Expand Down

0 comments on commit fc54de2

Please sign in to comment.