本文整理汇总了TypeScript中rxjs/Subscriber.Subscriber.create方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Subscriber.create方法的具体用法?TypeScript Subscriber.create怎么用?TypeScript Subscriber.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rxjs/Subscriber.Subscriber
的用法示例。
在下文中一共展示了Subscriber.create方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: watchProduct
watchProduct(productId: number): Observable<any> {
let openSubscriber = Subscriber.create(
() => this.webSocket.send({productId: productId}));
return this.webSocket.createObservableSocket('ws://localhost:8000', openSubscriber)
.map(message => JSON.parse(message));
}
示例2: constructor
constructor (url: string, protocol: string, openObserver?: Observer<Event>, closingObserver?: Observer<CloseEvent>) {
super();
this.url = url;
this.openObserver = openObserver;
this.closingObserver = closingObserver;
if (!WebSocket) { throw new TypeError('WebSocket not implemented in your runtime.'); }
this.websocket = new WebSocket(url);
var that = this;
var superNext = super.next;
var superError = super.error;
var superComplete = super.complete;
this.websocket.onopen = function (event) {
openObserver.next(event);
that.websocket.send("aaaa");
};
this.websocket.onclose = function (event) {
closingObserver.next(event);
};
this.websocket.onmessage = function (event) {
try {
superNext.call(that, event.data);
} catch (e) {
var errorEvent :ErrorEvent = new ErrorEvent(e);
errorEvent.message = "Invalid event structure.";
errorEvent.error = e;
superError.call(that, errorEvent);
}
};
this.websocket.onerror = function (event) {
superError.call(that, event);
};
this.destination = Subscriber.create(
(message: string) => {
this.websocket.send(message);
},
(error: any) => {
console.log("WebSocket error: " + error);
this.websocket.close(1011, "Error processing client data stream.");
var errorEvent :ErrorEvent = new ErrorEvent(error);
errorEvent.message = "Error processing client data stream.";
errorEvent.error = error;
superError.call(that, errorEvent);
}, //CloseEvent.code = Internal Error
() => {
console.log("WebSocket closing");
this.websocket.close(1011, "Error processing client data stream.");
var closeEvent :CloseEvent = new CloseEvent();
closeEvent.code = 1000; //CLOSE_NORMAL
closeEvent.reason = "WS connection closed by client.";
closeEvent.wasClean = true;
this.closingObserver.next(closeEvent);
}
);
}
示例3: constructor
constructor(url: string, protocol: string,
openObserver?: Observer<Event>,
closingObserver?: Observer<WebsocketCloseEvent>) {
super();
this.url = url;
this.openObserver = openObserver;
this.closingObserver = closingObserver;
if (!WebSocket) { throw new TypeError('WebSocket not implemented in your runtime.'); }
this.websocket = new WebSocket(this.url);
this.websocket.onopen = (event) => {
this.openObserver.next(event);
this.openObserver.complete();
};
this.websocket.onclose = (event) => {
this.closingObserver.next(event);
this.closingObserver.complete();
};
this.websocket.onmessage =
(event) => {
try {
super.next(event.data);
} catch (e) {
super.error(e);
}
};
this.websocket.onerror = (event) => {
super.error(event);
};
this.serverDestination = Subscriber.create(
(message: string) => {
this.websocket.send(message);
},
(error: any) => {
console.log('WebSocket error: ', error);
this.websocket.close(4011, 'Error processing client data stream.');
let errorEvent: WebsocketErrorEvent = new WebsocketErrorEvent();
errorEvent.message = 'Error processing client data stream.';
errorEvent.error = error;
super.error(errorEvent);
},
() => {
console.log('WebSocket closing');
this.websocket.close(1000, 'WS connection closed by client.');
let closeEvent: WebsocketCloseEvent = new WebsocketCloseEvent();
closeEvent.code = 1000; // CLOSE_NORMAL
closeEvent.reason = 'WS connection closed by client.';
closeEvent.wasClean = true;
this.closingObserver.next(closeEvent);
this.closingObserver.complete();
}
);
}
示例4: constructor
constructor() {
super('ws://' + window.location.host + '/ws', null,
Subscriber.create(event => console.log('socket open')),
Subscriber.create(event => console.log('socket is about to close'))
);
}