src/services/socket.service.ts
MirrorService class for interacting with the remote mirror collection.
constructor(config: SmartMirrorModuleConfig)
|
Defined in src/services/socket.service.ts:16
|
Creates the SocketService |
Public connect |
connect(mirrorId: void)
|
Defined in src/services/socket.service.ts:28
|
connects to the given socket.io service
Parameters :
Returns :
void
|
Public watch |
watch(message: void)
|
Defined in src/services/socket.service.ts:40
|
Watch to given message
Parameters :
Returns :
Observable<>
|
Public send |
send(message: void, obj: T)
|
Defined in src/services/socket.service.ts:56
|
Send message
Parameters :
Returns :
void
|
Private socket |
socket: |
Type : void
|
Defined in src/services/socket.service.ts:16
|
import {Injectable} from "@angular/core";
import {SmartMirrorModuleConfig} from "../utils/module.config";
import io from "socket.io-client";
import {Observable} from "rxjs";
/**
* MirrorService class for interacting with the remote mirror collection.
*/
@Injectable()
export class SocketService {
private socket: any;
/**
* Creates the SocketService
*/
constructor(private config: SmartMirrorModuleConfig) {
}
/**
* connects to the given socket.io service
* @param mirrorId optional mirror id to subscribe to
*/
public connect(mirrorId?: string) {
var cfg = null;
if (mirrorId)
cfg = {query: 'mirrorId=' + mirrorId};
this.socket = io(this.config.RTUrl, cfg);
}
/**
* Watch to given message
* @param message
*/
public watch<T>(message: string): Observable<T> {
return new Observable<T>(observer => {
this.socket.on(message, (data) => {
observer.next(data);
});
return () => {
this.socket.disconnect();
};
});
}
/**
* Send message
* @param message to send
* @param obj to send
*/
public send<T>(message: string, obj: T): void {
this.socket.emit(message, obj);
}
}