Created
August 27, 2016 14:51
-
-
Save vire/30af5a399075faf30bd83097108687c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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