本文整理汇总了TypeScript中rxjs/Rx.Subject.create方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Subject.create方法的具体用法?TypeScript Subject.create怎么用?TypeScript Subject.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rxjs/Rx.Subject
的用法示例。
在下文中一共展示了Subject.create方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: next
const createRxSocket = (connection) => {
let messages = Rx.Observable.fromEvent(connection, 'message', (message) => JSON.parse(message));
let messageObserver:any = {
next(message){
if(connection.readyState === 1){
connection.send(JSON.stringify(message));
}
}
}
connection.on('close', () => {
connection.streams && connection.streams.forEach(s => s.unsubscribe());
})
return Rx.Subject.create(messages, messageObserver);
}
示例2: create
private create(url): Subject<MessageEvent> {
let ws = new WebSocket(url);
let observable = Observable.create(
(obs: Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
}
);
let observer = {
next: (data: Object) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data));
}
},
};
return Subject.create(observer, observable);
}
示例3: create
private create(url): Rx.Subject<MessageEvent> {
let ws = new WebSocket(url);
// bind ws events to observable (streams)
let observable = Rx.Observable.create((obs: Rx.Observer<MessageEvent>) => {
ws.onmessage = obs.next.bind(obs);
ws.onerror = obs.error.bind(obs);
ws.onclose = obs.complete.bind(obs);
return ws.close.bind(ws);
}).share();
// on obs next (send something in the stream) send it using ws.
let observer = {
next: (data: Object) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(data));
}
},
};
return Rx.Subject.create(observer, observable);
}
示例4: connect
public connect(cb: () => void): Subject<Message> {
console.log('connecting...');
let self = this;
let observable = Observable.create((obs: Observer<Message>) => {
console.log('stomp client...');
self.client = Stomp.over(new SockJS(`${this.app.contextPath()}/client`));
// establish a connection to the server and subscribe for game events
self.client.connect({}, () => {
console.log('connected!');
self.client.subscribe(`/user/queue/game`, (msg) => obs.next(msg));
// notify listeners that we're connected now
self.stateSubject.next(SocketState.CONNECTED);
// invoke the connected callback up front
cb();
}, (err: any) => {
console.log(err);
self.stateSubject.next(SocketState.DISCONNECTED);
});
// clean-up by disconnecting if needed
return () => this.disconnect();
});
let observer = {
next: (message: Message) => {
self.client.send('/api/game', {}, JSON.stringify(message));
}
};
return Subject.create(observer, observable);
}