本文整理汇总了TypeScript中websocket.connection.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript connection.on方法的具体用法?TypeScript connection.on怎么用?TypeScript connection.on使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类websocket.connection
的用法示例。
在下文中一共展示了connection.on方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: constructor
constructor(connection: WebSocketConnection) {
this.connection_ = connection;
this.onMessage_ = new Subject<SocketMessage>();
this.onClose_ = new Subject<void>();
this.onError_ = new Subject<Error>();
connection.on("message", (message: WebSocketMessage) => {
if (message.type == "utf8") {
this.onMessage_.onNext({ string: message.utf8Data });
} else if (message.type == "binary") {
this.onMessage_.onNext({ data: bufferToData(message.binaryData) });
} else {
throw new Error(`invalid message ${message.type}`);
}
});
connection.on("close", (code: number, desc: string) => {
this.onClose_.onNext(null);
});
connection.on("error", (error: Error) => {
this.handleError(error);
});
}
示例2: resolve
let responsePromise: P<ServiceResponse> = new P<ServiceResponse>((resolve) => {
var h = (msg) => {
console.log("Receive message", msg.type);
// Disconnect our listener
this.conn.removeListener('message', h);
if(msg.type === 'binary') {
console.log("Received binary message of len = ", msg.binaryData.length);
var response = this.protocolHandler.decode(msg.binaryData);
resolve(new BaseServiceResponse(response));
}
};
this.conn.on('message', h);
});