import { Spi } from "./Spi"; export const ConnectionState = { Disconnected: 'Disconnected', Connecting: 'Connecting', Connected: 'Connected' }; export const MXA_VERSION = 290 export const SPI_PROTOCOL = `spi.${MXA_VERSION.toString().split('').join('.')}`; export class ConnectionStateEventArgs { ConnectionState: string; constructor(connectionState: string) { this.ConnectionState = connectionState; } } export class MessageEventArgs { public Message: any; constructor(message: any) { this.Message = message; } } export class Connection { public Address: string; public Connected: boolean; public SpiProtocol: string; public State: string; public _conectionTimeout: any; public _connectionTimeout: any; public _spi: any; public _ws: any; constructor(spi: Spi) { this.Address = null; this.Connected = false; this.State = ConnectionState.Disconnected; this.SpiProtocol = SPI_PROTOCOL; this._spi = spi; this._ws = null; this._conectionTimeout = null; if(typeof WebSocket === 'undefined') { throw new Error('Environment does not support WebSockets'); } } _cancelConnectionTimeout() { if (this._conectionTimeout) { clearTimeout(this._conectionTimeout); this._connectionTimeout = null; } } Connect() { if(this.State === ConnectionState.Connected || this.State === ConnectionState.Connecting) { // already connected or connecting. disconnect first. return; } this.State = ConnectionState.Connecting; //Create a new socket instance specifying the url, SPI protocol and Websocket to use. //The will create a TCP/IP socket connection to the provided URL and perform HTTP websocket negotiation this._ws = new WebSocket(this.Address, this.SpiProtocol); this._ws.onopen = () => this.pollWebSocketConnection(); this._ws.onmessage = (payload: any) => this.onMessageReceived(payload); this._ws.onclose = () => this.onClosed(); this._ws.onerror = (err: any) => this.onError(err); const timeoutConnectionAttempt = () => { if (this._ws && this.State === ConnectionState.Connecting) { this.Disconnect(); } } this._conectionTimeout = setTimeout(timeoutConnectionAttempt, 4000); this._spi._eventBus.dispatchEvent(new CustomEvent('ConnectionStatusChanged', {detail: new ConnectionStateEventArgs(ConnectionState.Connecting)})); } Disconnect() { if (this.State == ConnectionState.Disconnected) return; if(this._ws && this._ws.readyState != this._ws.CLOSED) { this._ws.close(); } if (this._ws) { this._ws.onopen = null; this._ws.onmessage = null; this._ws.onclose = null; this._ws.onerror = null; } this.onClosed(); } Send(message: any) { this._ws.send(message); } onOpened() { this._cancelConnectionTimeout(); this.State = ConnectionState.Connected; this.Connected = true; this._spi._eventBus.dispatchEvent(new CustomEvent('ConnectionStatusChanged', {detail: new ConnectionStateEventArgs(ConnectionState.Connected)})); } onClosed() { this._cancelConnectionTimeout(); if (this.Connected === false && this.State === ConnectionState.Disconnected) return; this.Connected = false; this.State = ConnectionState.Disconnected; this._ws = null; this._spi._eventBus.dispatchEvent(new CustomEvent('ConnectionStatusChanged', {detail: new ConnectionStateEventArgs(ConnectionState.Disconnected)})); } pollWebSocketConnection(count = 0) { // Timeout trying to connect after 20 * 200ms = 4000 ms if(this._ws.readyState === this._ws.OPEN) { this.onOpened(); return true; } else if(count < 20) { count++; setTimeout(() => this.pollWebSocketConnection(count), 200); } else { this.Disconnect(); return false; } } onMessageReceived(message: any) { this._spi._eventBus.dispatchEvent(new CustomEvent('MessageReceived', {detail: new MessageEventArgs(message.data)})); } onError(err: any) { this._cancelConnectionTimeout(); this._spi._eventBus.dispatchEvent(new CustomEvent('ErrorReceived', {detail: new MessageEventArgs(err)})); } }