本文整理汇总了TypeScript中ws.on函数的典型用法代码示例。如果您正苦于以下问题:TypeScript on函数的具体用法?TypeScript on怎么用?TypeScript on使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了on函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: it
it("did not close connection when ping event is successfully received", function (done) {
this.timeout(20 * 1000)
let ping_times = 0
let success = false
ws1.on('message', (data) => {
const {ev, value} = JSON.parse(data)
switch(ev) {
case "PING":
const pong = {
"ev": "PONG",
"value": value
}
try {
ws1.send(JSON.stringify(pong))
} catch(e) {
// nothing to do
}
ping_times++
break
}
if (ping_times > 1) {
success = true
ws1.close()
}
})
ws1.on('close', () => {
assert.equal(success, true); done()
})
})
示例2: open
public open() {
console.log("TunatorConnector {open:", this.config.url);
this.client = new WebSocket(this.config.url);
this.client.on('error', (error) => {
console.error("TunatorConnector Connection Error: " + error.toString() + " reconnect");
// setTimeout(() => { tc.open() }, 1000);
});
this.client.on('close', () => {
console.log("TunatorConnector Connection Closed: reconnect");
setTimeout(() => { this.open() }, 1000);
});
this.client.on('message', (data, flags) => {
//let pacType = Packet.receive(data);
// console.log(">>>TC:", flags, Buffer.from(data).toString());
this.processMessage(null, Buffer.from(data));
});
this.client.on('open', () => {
console.log('TunatorConnector WebSocket Client Connected', this.config.myAddr.asJson());
Packet.Packet.sendJson(this.client, "init", this.config.myAddr);
});
this.on("receiveJSON", (ws: WebSocket, jPack: Packet.JsonPacket) => {
console.log("jPack", jPack);
if (jPack.action == "init-res") {
let tunDevice = TunDevice.fromJson(jPack.data);
if (tunDevice.tunDevName == "") {
}
// if (jPack)
}
});
console.log("TunatorConnector }open:", this.config.url);
}
示例3: constructor
constructor(httpPort: string, onConnected: () => any, onMsg: (msg: string) => any, serverVersion: string = "1.0") {
let log = loglevel.getLogger('ensime-client');
// Since 2.0
if(serverVersion && serverVersion >= "2")
this.websocket = new WebSocket("ws://localhost:" + httpPort + "/websocket", ["jerky"])
else
this.websocket = new WebSocket("ws://localhost:" + httpPort + "/jerky");
this.websocket.on("open", () => {
log.debug("connecting websocketâŚ");
onConnected();
});
this.websocket.on("message", (msg) => {
log.debug(`incoming: ${msg}`)
onMsg(msg);
});
this.websocket.on("error", (error) => {
log.error(error);
});
this.websocket.on("close", () => {
log.debug("websocket closed from server");
});
}
示例4: onMsg
export const wrapWebSocket = <R, W>(socket: WebSocket): [TinyReader<R>, TinyWriter<W>] => {
// TODO: Consider using the code in wsclient instead to convert a socket to a reader/writer pair.
const reader: TinyReader<R> = {buf: [], isClosed: false}
socket.on("message", data => {
// if (!isProd) console.log('C->S', data)
onMsg(reader, JSON.parse(data as any))
})
// I could just go ahead and make a TinyWriter, but this handles
// backpressure.
const writer = new Writable({
objectMode: true,
write(data, _, callback) {
// if (!isProd) console.log('S->C', data)
if (socket.readyState === socket.OPEN) {
// TODO: Should this pass the callback? Will that make backpressure
// work?
socket.send(JSON.stringify(data))
}
callback()
},
})
socket.on('close', () => {
writer.end() // Does this help??
reader.isClosed = true
reader.onClose && reader.onClose()
})
return [reader, wrapWriter(writer)]
}
示例5: _handleWebSocket
private _handleWebSocket(client: WebSocket) {
client.on('message', data => {
this.executor.log('Received WebSocket message');
const message: Message = JSON.parse(data.toString());
this._handleMessage(message)
.catch(error => this.executor.emit('error', error))
.then(() => {
this.executor.log('Sending ack for [', message.id, ']');
client.send(JSON.stringify({ id: message.id }), error => {
if (error) {
this.executor.emit(
'error',
new Error(
`Error sending ack for [ ${message.id} ]: ${error.message}`
)
);
}
});
});
});
client.on('error', error => {
this.executor.log('WebSocket client error:', error);
this.executor.emit('error', error);
});
}
示例6: connectToServer
function connectToServer() {
ws = new WebSocket("ws://localhost:8080");
ws.on('open', function open() {
console.log(colors.magenta("Connected to ws://localhost:8080"));
console.log(separator);
console.log("");
recurse();
});
ws.on("error", () => {
console.log(colors.red("No server running."));
console.log(colors.magenta("Starting server.."));
server = require("./server");
connectToServer();
});
ws.on('message', function(data, flags) {
// flags.binary will be set if a binary data is received.
// flags.masked will be set if the data was masked.
let parsed = JSON.parse(data);
if(parsed.kind === "code error") {
console.error(colors.red(parsed.data));
} else if(parsed.kind === "code result") {
console.log(resultsTable(parsed.data));
} else if(parsed.kind === "code changeset") {
console.log(`${parsed.data} ${pluralize("row", parsed.data)} added/removed`);
} else {
return;
}
console.log("");
console.log(separator);
console.log("");
recurse();
});
}
示例7: _handleWebSocket
private _handleWebSocket(client: WebSocket) {
client.on('message', data => {
this.executor.log('Received WebSocket message');
const message: Message = JSON.parse(data);
this._handleMessage(message)
.catch(error => this.executor.emit('error', error))
.then(() => {
// Don't send acks for runEnd, because by the remote will
// hev been shut down by the time we get here.
if (message.name !== 'runEnd') {
this.executor.log('Sending ack for [', message.id, ']');
client.send(
JSON.stringify({ id: message.id }),
error => {
if (error) {
this.executor.emit(
'error',
new Error(
`Error sending ack for [ ${message.id} ]: ${error.message}`
)
);
}
}
);
}
});
});
client.on('error', error => {
this.executor.log('WebSocket client error:', error);
this.executor.emit('error', error);
});
}
示例8:
UserManager.get.loggedIn( ws.upgradeReq, null ).then(( user ) => {
this.ws = ws;
this.user = user;
ws.on( 'message', this.onMessage.bind( this ) );
ws.on( 'close', this.onClose.bind( this ) );
ws.on( 'error', this.onError.bind( this ) );
} ).catch( this.onError );