Skip to content

Instantly share code, notes, and snippets.

@vire
Created August 27, 2016 14:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vire/30af5a399075faf30bd83097108687c7 to your computer and use it in GitHub Desktop.
Save vire/30af5a399075faf30bd83097108687c7 to your computer and use it in GitHub Desktop.
// @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