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