diff --git a/lib/RealTimeAPI.d.ts b/lib/RealTimeAPI.d.ts new file mode 100644 index 0000000..a2143d8 --- /dev/null +++ b/lib/RealTimeAPI.d.ts @@ -0,0 +1,50 @@ +/** + * Rocket.Chat RealTime API + */ +import { Observable } from "rxjs"; +import { WebSocketSubject } from 'rxjs/observable/dom/WebSocketSubject'; +export declare class RealTimeAPI { + url: string; + webSocket: WebSocketSubject<{}>; + constructor(param: string | WebSocketSubject<{}>); + /** + * Returns the Observable to the RealTime API Socket + */ + getObservable(): Observable; + /** + * onMessage + */ + onMessage(messageHandler?: ((value: {}) => void) | undefined): void; + /** + * onError + */ + onError(errorHandler?: ((error: any) => void) | undefined): void; + /** + * onCompletion + */ + onCompletion(completionHandler?: (() => void) | undefined): void; + /** + * Subscribe to the WebSocket of the RealTime API + */ + subscribe(messageHandler?: ((value: {}) => void) | undefined, errorHandler?: ((error: any) => void) | undefined, completionHandler?: (() => void) | undefined): void; + /** + * sendMessage to Rocket.Chat Server + */ + sendMessage(messageObject: {}): void; + /** + * getObservableFilteredByMessageType + */ + getObservableFilteredByMessageType(messageType: string): Observable; + /** + * getObservableFilteredByID + */ + getObservableFilteredByID(id: string): Observable; + /** + * connectToServer + */ + connectToServer(): Observable; + /** + * keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive. + */ + keepAlive(): void; +} diff --git a/lib/RealTimeAPI.js b/lib/RealTimeAPI.js new file mode 100644 index 0000000..87a58ed --- /dev/null +++ b/lib/RealTimeAPI.js @@ -0,0 +1,87 @@ +"use strict"; +/** + * Rocket.Chat RealTime API + */ +Object.defineProperty(exports, "__esModule", { value: true }); +var rxjs_1 = require("rxjs"); +var RealTimeAPI = (function () { + function RealTimeAPI(param) { + switch (typeof param) { + case "string": + this.url = param; + this.webSocket = rxjs_1.Observable.webSocket(this.url); + break; + case "object": + this.webSocket = param; + this.url = this.webSocket.url; + break; + default: + throw new Error("Invalid Parameter to the Constructor, Parameter must be of Type WebSocketSubject or URL but was found of type \"" + typeof param + "\""); + } + } + /** + * Returns the Observable to the RealTime API Socket + */ + RealTimeAPI.prototype.getObservable = function () { + return this.webSocket.catch(function (err) { return rxjs_1.Observable.of(err); }); + }; + /** + * onMessage + */ + RealTimeAPI.prototype.onMessage = function (messageHandler) { + this.subscribe(messageHandler, undefined, undefined); + }; + /** + * onError + */ + RealTimeAPI.prototype.onError = function (errorHandler) { + this.subscribe(undefined, errorHandler, undefined); + }; + /** + * onCompletion + */ + RealTimeAPI.prototype.onCompletion = function (completionHandler) { + this.subscribe(undefined, undefined, completionHandler); + }; + /** + * Subscribe to the WebSocket of the RealTime API + */ + RealTimeAPI.prototype.subscribe = function (messageHandler, errorHandler, completionHandler) { + this.getObservable().subscribe(messageHandler, errorHandler, completionHandler); + }; + /** + * sendMessage to Rocket.Chat Server + */ + RealTimeAPI.prototype.sendMessage = function (messageObject) { + this.webSocket.next(JSON.stringify(messageObject)); + }; + /** + * getObservableFilteredByMessageType + */ + RealTimeAPI.prototype.getObservableFilteredByMessageType = function (messageType) { + return this.getObservable().filter(function (message) { return message.msg === messageType; }); + }; + /** + * getObservableFilteredByID + */ + RealTimeAPI.prototype.getObservableFilteredByID = function (id) { + return this.getObservable().filter(function (message) { return message.id === id; }); + }; + /** + * connectToServer + */ + RealTimeAPI.prototype.connectToServer = function () { + this.sendMessage({ "msg": "connect", "version": "1", "support": ["1", "pre2", "pre1"] }); + return this.getObservableFilteredByMessageType("connected"); + }; + /** + * keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive. + */ + RealTimeAPI.prototype.keepAlive = function () { + var _this = this; + this.getObservableFilteredByMessageType("ping").subscribe(function (message) { return _this.sendMessage({ msg: "pong" }); }); + }; + return RealTimeAPI; +}()); +exports.RealTimeAPI = RealTimeAPI; +//# sourceMappingURL=RealTimeAPI.js.map \ No newline at end of file diff --git a/lib/RealTimeAPI.js.map b/lib/RealTimeAPI.js.map new file mode 100644 index 0000000..fd701b7 --- /dev/null +++ b/lib/RealTimeAPI.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,kCAAW"} \ No newline at end of file diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..854370d --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1 @@ +export { RealTimeAPI } from "./RealTimeAPI"; diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..1d31cab --- /dev/null +++ b/lib/index.js @@ -0,0 +1,5 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +var RealTimeAPI_1 = require("./RealTimeAPI"); +exports.RealTimeAPI = RealTimeAPI_1.RealTimeAPI; +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/lib/index.js.map b/lib/index.js.map new file mode 100644 index 0000000..d19aab3 --- /dev/null +++ b/lib/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA,6CAA4C;AAAnC,oCAAA,WAAW,CAAA"} \ No newline at end of file diff --git a/src/RealTimeAPI.ts b/src/RealTimeAPI.ts new file mode 100644 index 0000000..4ed8413 --- /dev/null +++ b/src/RealTimeAPI.ts @@ -0,0 +1,100 @@ +/** + * Rocket.Chat RealTime API + */ + +import { Observable } from "rxjs"; +import { WebSocketSubject } from 'rxjs/observable/dom/WebSocketSubject'; + +export class RealTimeAPI { + public url: string; + public webSocket: WebSocketSubject<{}>; + + constructor(param: string | WebSocketSubject<{}>) { + switch (typeof param) { + case "string": + this.url = param as string; + this.webSocket = Observable.webSocket(this.url); + break; + case "object": + this.webSocket = param as WebSocketSubject<{}>; + this.url = this.webSocket.url; + break; + default: + throw new Error(`Invalid Parameter to the Constructor, Parameter must be of Type WebSocketSubject or URL but was found of type "${typeof param}"`); + } + } + + /** + * Returns the Observable to the RealTime API Socket + */ + public getObservable() { + return this.webSocket.catch(err => Observable.of(err)); + } + + /** + * onMessage + */ + public onMessage(messageHandler?: ((value: {}) => void) | undefined): void { + this.subscribe(messageHandler, undefined, undefined); + } + + + /** + * onError + */ + public onError(errorHandler?: ((error: any) => void) | undefined): void { + this.subscribe(undefined, errorHandler, undefined); + } + + /** + * onCompletion + */ + public onCompletion(completionHandler?: (() => void) | undefined): void { + this.subscribe(undefined, undefined, completionHandler); + } + + /** + * Subscribe to the WebSocket of the RealTime API + */ + public subscribe(messageHandler?: ((value: {}) => void) | undefined, errorHandler?: ((error: any) => void) | undefined, completionHandler?: (() => void) | undefined) { + this.getObservable().subscribe(messageHandler, errorHandler, completionHandler); + } + + /** + * sendMessage to Rocket.Chat Server + */ + public sendMessage(messageObject: {}): void { + this.webSocket.next(JSON.stringify(messageObject)); + } + + /** + * getObservableFilteredByMessageType + */ + public getObservableFilteredByMessageType(messageType: string) { + return this.getObservable().filter((message: any) => message.msg === messageType); + } + + /** + * getObservableFilteredByID + */ + public getObservableFilteredByID(id: string) { + return this.getObservable().filter((message: any) => message.id === id); + } + + /** + * connectToServer + */ + public connectToServer() { + this.sendMessage({ "msg": "connect", "version": "1", "support": ["1", "pre2", "pre1"] }); + return this.getObservableFilteredByMessageType("connected"); + } + + /** + * keepAlive, Ping and Pong to the Rocket.Chat Server to Keep the Connection Alive. + */ + public keepAlive(): void { + this.getObservableFilteredByMessageType("ping").subscribe( + message => this.sendMessage({ msg: "pong" }) + ); + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..28ebbb7 --- /dev/null +++ b/src/index.ts @@ -0,0 +1 @@ +export { RealTimeAPI } from "./RealTimeAPI"; \ No newline at end of file