initialize() { let retries : number = 0; let max_retries : number = process.env.NODE_ENV === 'production' ? 5 : 1; this.logger = SystemLogger.get(); // Create the web socket this.socket = new Client(); this.socket.on('connectFailed', (error: Error) => { console.log("Platform is not available yet. Retrying in 2 seconds"); retries++; if(retries <= max_retries) { setTimeout(() => { // Establish connection to the platform this.socket.connect('ws://'+this.host+":"+this.port, "v1"); }, 2000); } else { this.connRejecter(error); } }); this.socket.on('connect', (conn: any) => { console.log("Connected to stream"); conn.on('error', this.handleConnectionError.bind(this)); conn.on('close', function() { console.log('v1 Connection Closed'); }); this.connResolver(conn); }); // Establish connection to the platform this.socket.connect('ws://'+this.host+":"+this.port, "v1"); }
function connect(address: string, retry: boolean) { if (retry) { info('retrying...') } const ws = new WebSocketClient() ws.on('connectFailed', function (err) { error('Could not connect to server ' + Config.server + ': ' + err.stack) info('retrying in one minute') setTimeout(function () { connect(address, true) }, 60000) }) ws.on('connect', function (con) { Connection = con ok('connected to server ' + Config.server) con.on('error', function (err) { error('connection error: ' + err.stack) }) con.on('close', function (code, reason) { // Is this always error or can this be intended...? error('connection closed: ' + reason + ' (' + code + ')') info('retrying in one minute') for (const i in users) { delete users[i] } rooms.clear() setTimeout(function () { connect(address, true) }, 60000) }) con.on('message', function (response) { if (response.type !== 'utf8') return false const message = response.utf8Data recv(message) parseData(message) }) }) info('connecting to ' + address + ' - secondary protocols: ' + (Config.secprotocols.join(', ') || 'none')) ws.connect(address, Config.secprotocols) }
let promise = new Promise<void>((resolve, reject) => { var wsClient = new WebSocket.client(); wsClient.on('connect', function(connection) { connection.on('error', function(error) { console.log("Connection Error: " + error.toString()); reject(error); }); connection.on('close', function() { console.log('echo-protocol Connection Closed'); }); connection.on('message', function(message) { if (message.type === 'utf8') { let json = JSON.parse(message.utf8Data); if(json.type === "snapshot:init") { let data : INSocket.IStatusInit = json; nobilApp.parseDataFromInitialStatusNobilWebSocket(data).then(() => { console.log("Initial data parsed!"); resolve(null); }); } else if(json.type === "status:update") { let data : INSocket.IStatusUpdate = json; nobilApp.updateChargingStationWithLiveUpdate(data); } else if(json.type === "status:raw") { // Do nothing for now // let data: INSocket.IStatusRaw = json; } } }); }); wsClient.connect('ws://realtime.nobil.no/api/v1/stream?apikey=' + Secrets.apiKey); });
const connector = (endpoint: string): Promise<WebSocket.connection> => new Promise((resolve, reject) => { const client = new WebSocket.client(); client.on('connectFailed', (error) => reject(error)); client.on('connect', (connection) => resolve(connection)); client.connect(endpoint); });
}); wsRouter.mount(/^route\/[a-zA-Z]+$/, (request) => { }); wsRouter.unmount('*'); wsRouter.unmount('*', 'protocol'); wsRouter.unmount(/^route\/[a-zA-Z]+$/); wsRouter.unmount(/^route\/[a-zA-Z]+$/, 'protocol'); } { var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('connectFailed', (error: Error) => { console.log('Connect Error: ' + error.toString()); }); client.on('connect', (connection: websocket.connection) => { console.log('WebSocket client connected'); connection.on('error', (error: Error) => { console.log("Connection Error: " + error.toString()); }); connection.on('close', () => { console.log('echo-protocol Connection Closed'); });
setTimeout(() => { // Establish connection to the platform this.socket.connect('ws://'+this.host+":"+this.port, "v1"); }, 2000);