本文整理匯總了TypeScript中websocket.client類的典型用法代碼示例。如果您正苦於以下問題:TypeScript client類的具體用法?TypeScript client怎麽用?TypeScript client使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了client類的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: if
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);
});
示例2: Promise
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);
});
示例3: require
});
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');
});
示例4: connect
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)
}
示例5: initialize
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");
}
示例6: setTimeout
setTimeout(() => {
// Establish connection to the platform
this.socket.connect('ws://'+this.host+":"+this.port, "v1");
}, 2000);