Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// @author Markus Werle @daixtrose
// snippet based on RxJS5+WebSockets discussion in https://gitter.im/Reactive-Extensions/RxJS room
import * as io from 'socket.io-client';
export class SocketService {
public url : string;
private _receivedMessagesRelay: Subject<{}>;
public receivedMessages: Observable<{}>;
// ....
socket: SocketIOClient.Socket;
constructor() {
this.url = "ws://192.168.173.1:3000";
this.socket = null;
this._receivedMessagesRelay = new Subject<{}>();
this.receivedMessages = this._receivedMessagesRelay.asObservable();
}
connect() {
this.socket = io.connect(this.url);
}
disconnect() {
this.socket.disconnect();
this.socket = null;
}
private _initializeSocketLogging() {
this.socket.on("connect", (msg) => {
this._log("Connection Status", "Device connected: " + JSON.stringify(msg));
});
// socket.io docs .
// ...
this.socket.on("message", (msg) => {
this._receivedMessagesRelay.next(JSON.stringify(msg));
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.