本文整理汇总了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);