mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-20 21:35:50 +00:00
save
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import { HttpHeaders, HttpParams } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Observable } from "rxjs"
|
||||
import { AuthService } from './auth.service';
|
||||
import { HttpService } from './http.service';
|
||||
import { StorageService } from './storage.service';
|
||||
import { HttpClient, HttpHeaderResponse } from '@angular/common/http';
|
||||
import { environment } from 'src/environments/environment';
|
||||
import { Storage } from '@ionic/storage';
|
||||
import { Message } from 'src/app/models/message.model';
|
||||
import { Observable, Subject } from "rxjs/Rx";
|
||||
import { WebsocketService } from './websocket.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
@@ -17,6 +19,9 @@ export class ChatService {
|
||||
options1:any;
|
||||
X_User_Id:any;
|
||||
X_Auth_Token:any;
|
||||
|
||||
SERVER_URL = 'wss://www.tabularium.pt/websocket';
|
||||
public messages: Subject<any>;
|
||||
|
||||
|
||||
constructor(
|
||||
@@ -24,11 +29,11 @@ export class ChatService {
|
||||
private httpService: HttpService,
|
||||
private authService: AuthService,
|
||||
private storage: Storage,
|
||||
private storageService:StorageService,) {
|
||||
private storageService:StorageService,
|
||||
private wsService: WebsocketService,
|
||||
)
|
||||
{
|
||||
this.headers = new HttpHeaders();
|
||||
/* this.headers = this.headers.set('X-User-Id', 'GqjNWiLrGEHRna7Zn');
|
||||
this.headers = this.headers.set('X-Auth-Token', 'dAM0ZOTAy8jzQA_vS25z2IrnSc6sYLfi5rmaa35YNUz'); */
|
||||
-
|
||||
this.authService.userData$.subscribe((res:any)=>{
|
||||
this.headers = this.headers.set('X-User-Id', res.userId);
|
||||
this.headers = this.headers.set('X-Auth-Token', res.authToken);
|
||||
@@ -36,6 +41,16 @@ export class ChatService {
|
||||
this.options = {
|
||||
headers: this.headers,
|
||||
};
|
||||
|
||||
this.messages = <Subject<any>>this.wsService.connect(this.SERVER_URL).map((response: MessageEvent): any => {
|
||||
let data = JSON.parse(response.data);
|
||||
console.log(data);
|
||||
|
||||
return {
|
||||
id: data.id,
|
||||
msg: data.msg
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/* getUser(){
|
||||
|
||||
@@ -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