mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 21:06:06 +00:00
save
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import * as io from 'socket.io-client';
|
||||
import { Observable } from 'rxjs';
|
||||
import { Socket } from 'ngx-socket-io';
|
||||
import * as Rx from "rxjs/Rx";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -11,11 +9,35 @@ export class WebsocketService {
|
||||
message = '';
|
||||
messages = [];
|
||||
currentUser = '';
|
||||
private subject: Rx.Subject<MessageEvent>;
|
||||
|
||||
constructor(private socket: Socket) { }
|
||||
constructor() { }
|
||||
|
||||
connect(){
|
||||
return this.socket.connect();
|
||||
public connect(url): Rx.Subject<MessageEvent> {
|
||||
if (!this.subject) {
|
||||
this.subject = this.create(url);
|
||||
console.log("Successfully connected: " + url);
|
||||
}
|
||||
return this.subject;
|
||||
}
|
||||
|
||||
private create(url): Rx.Subject<MessageEvent> {
|
||||
let ws = new WebSocket(url);
|
||||
|
||||
let observable = Rx.Observable.create((obs: Rx.Observer<MessageEvent>) => {
|
||||
ws.onmessage = obs.next.bind(obs);
|
||||
ws.onerror = obs.error.bind(obs);
|
||||
ws.onclose = obs.complete.bind(obs);
|
||||
return ws.close.bind(ws);
|
||||
});
|
||||
let observer = {
|
||||
next: (data: Object) => {
|
||||
if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.send(JSON.stringify(data));
|
||||
}
|
||||
}
|
||||
};
|
||||
return Rx.Subject.create(observer, observable);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user