Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var WebSocket = require('ws');
- class WebsocketClient {
- constructor(headers) {
- this.uri_ = "";
- this.con_ = null;
- this.headers_ = headers || {};
- this.reconnect_timeout_ref_ = null;
- this.reconnect_timeout_ = 500;
- this.onText = null;
- this.onBytes = null;
- this.onConnect = null;
- this.onDisconnect = null;
- }
- isConnected() {
- return this.con_ && (this.con_.readyState === this.con_.OPEN);
- }
- writeBytes(bytes) {
- if (!this.isConnected()) {
- return;
- }
- this.con_.send(bytes);
- }
- writeText(text) {
- if (!this.isConnected()) {
- return;
- }
- this.con_.send(text);
- }
- sendPing() {
- this.writeText("2");
- }
- startReconnectTimer(instant = false) {
- if (this.reconnect_timeout_ref_) {
- return;
- }
- const self = this;
- let timer_cb = function () {
- self.reconnect_timeout_ref_ = null;
- self.reconnect_timeout_ *= 2;
- if (self.reconnect_timeout_ > 15000) {
- self.reconnect_timeout_ = 15000;
- }
- self.setUpConnection();
- self.startReconnectTimer();
- };
- if (instant) {
- timer_cb();
- }
- else {
- //console.log("trying to reconnect ws in " + this.reconnect_timeout_.toString() + "ms...");
- this.reconnect_timeout_ref_ = setTimeout(timer_cb, this.reconnect_timeout_);
- }
- }
- stopReconnectTimer() {
- if (this.reconnect_timeout_ref_) {
- clearInterval(this.reconnect_timeout_ref_);
- this.reconnect_timeout_ref_ = null;
- this.reconnect_timeout_ = 500;
- console.log("stopped reconnect timer!");
- }
- }
- connect(uri) {
- this.uri_ = uri;
- this.startReconnectTimer(true);
- }
- clearConnection() {
- if (!this.con_) {
- return;
- }
- this.con_.onopen = function () { };
- this.con_.onerror = function (error) { };
- this.con_.onmessage = function (e) { };
- this.con_.onclose = function () { };
- this.con_.close();
- this.con_ = null;
- this.onDisconnect && this.onDisconnect();
- }
- close() {
- this.stopReconnectTimer();
- this.clearConnection();
- }
- setUpConnection() {
- this.clearConnection();
- const self = this;
- this.con_ = new WebSocket(this.uri_, {
- headers: self.headers_
- });
- this.con_.binaryType = "arraybuffer";
- this.con_.onopen = function () {
- self.onConOpened();
- };
- this.con_.onerror = function (err) {
- self.onConError(err);
- };
- /*
- this.ng_zone_.runOutsideAngular(() => {
- this.con_.onmessage = function (e: MessageEvent) {
- self.onConMessageReceived(e);
- };
- });
- */
- this.con_.onmessage = function (e) {
- self.onConMessageReceived(e);
- };
- this.con_.onclose = function () {
- self.onConClosed();
- }
- }
- onConOpened() {
- //console.log("ws connected!");
- this.stopReconnectTimer();
- this.onConnect && this.onConnect();
- }
- onConError(err) {
- //console.log("ws error!", err);
- this.clearConnection();
- this.startReconnectTimer();
- }
- onConClosed() {
- //console.log("ws closed");
- this.clearConnection();
- this.startReconnectTimer();
- }
- onConMessageReceived(e) {
- var data = e.data;
- if (typeof data === "string") {
- this.onText && this.onText(data);
- }
- else {
- this.onBytes && this.onBytes(data);
- }
- }
- }
- module.exports = WebsocketClient;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement