本文整理匯總了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);
});