Finishes basic websocket-handler.service.ts

This commit is contained in:
GloriousCode
2017-09-12 19:46:57 +10:00
parent 6d02dfe057
commit dfc6b0e392
6 changed files with 64 additions and 34 deletions

View File

@@ -10,11 +10,17 @@ export class WebsocketService {
public connect(url): Rx.Subject<MessageEvent> {
if (!this.subject) {
this.subject = this.create(url);
console.log("Successfully connected: " + url);
}
return this.subject;
}
private authenticateMessage = {
Event:'auth',
data:{"username":"Username","password":"e7cf3ef4f17c3999a94f2c6f612e8a888e5b1026878e4e19398b23bd38ec221a"},
}
private isAuth = false;
private create(url): Rx.Subject<MessageEvent> {
let ws = new WebSocket(url);
@@ -27,12 +33,16 @@ export class WebsocketService {
})
let observer = {
next: (data: Object) => {
if (ws.readyState === WebSocket.OPEN) {
if (ws.readyState === WebSocket.OPEN) {
if(!this.isAuth) {
//This is a shit initial way to be able to authenticate
ws.send(JSON.stringify(this.authenticateMessage));
this.isAuth = true;
}
ws.send(JSON.stringify(data));
}
}
}
}
}
}
return Rx.Subject.create(observer, observable);
}
}