reconnect

This commit is contained in:
Peter Maquiran
2022-01-12 13:47:17 +01:00
parent 45284fc1a6
commit 8aec66bcaf
2 changed files with 24 additions and 40 deletions
+4 -17
View File
@@ -37,19 +37,13 @@ export class AuthService {
this.ValidatedUser = SessionStore.user this.ValidatedUser = SessionStore.user
console.log('login', SessionStore.user.RochetChatUser, SessionStore.user.Password) console.log('login', SessionStore.user.RochetChatUser, SessionStore.user.Password)
this.WsChatService.connect(()=>{ this.WsChatService.connect();
this.WsChatService.login().then((message) => {
this.WsChatService.login({
username: SessionStore.user.RochetChatUser,
password: SessionStore.user.Password
}).then((message) => {
console.log('rocket chat login successfully', message) console.log('rocket chat login successfully', message)
}).catch((message)=>{ }).catch((message)=>{
console.log('rocket chat login failed', message) console.log('rocket chat login failed', message)
}) })
})
} }
if (localStorage.getItem("userChat") != null) { if (localStorage.getItem("userChat") != null) {
@@ -116,18 +110,11 @@ export class AuthService {
} }
this.WsChatService.connect(()=>{ this.WsChatService.connect();
this.WsChatService.login().then((message) => {
this.WsChatService.login({
username: SessionStore.user.RochetChatUser,
password: user.password
}).then((message) => {
console.log('rocket chat login successfully', message) console.log('rocket chat login successfully', message)
}).catch((message)=>{ }).catch((message)=>{
console.log('rocket chat login failed', message) console.log('rocket chat login failed', message)
}).finally(()=>{
})
}) })
+13 -16
View File
@@ -3,21 +3,20 @@ import { v4 as uuidv4 } from 'uuid'
import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service' import { wsCallbacksParams, msgQueue } from 'src/app/models/rochet-chat-cliente-service'
import { deepFind } from 'src/plugin/deep' import { deepFind } from 'src/plugin/deep'
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
import { SessionStore } from 'src/app/store/session.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class WsChatService { export class WsChatService {
private firstPingFunx: Function
isLogin = false; isLogin = false;
constructor() {} constructor() {}
connect(firstPingFunx: Function) { connect() {
this.firstPingFunx = firstPingFunx //
this.ws.connect(environment.apiWsChatUrl); this.ws.connect();
const connectMessage = { const connectMessage = {
msg: "connect", msg: "connect",
@@ -27,14 +26,10 @@ export class WsChatService {
this.ws.send(connectMessage, 'connect', false) this.ws.send(connectMessage, 'connect', false)
this.ws.send({msg:"pong"},'recoonect', false) this.ws.send({msg:"pong"},'recoonect', false)
firstPingFunx()
} }
login(user) { login() {
const requestId = uuidv4() const requestId = uuidv4()
this.isLogin = false
const loginRequest = { const loginRequest = {
msg: "method", msg: "method",
@@ -42,8 +37,8 @@ export class WsChatService {
id: requestId, id: requestId,
params: [ params: [
{ {
user: { username: user.username }, user: { username: SessionStore.user.RochetChatUser },
password: user.password password: SessionStore.user.Password
} }
] ]
} }
@@ -52,6 +47,7 @@ export class WsChatService {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{ this.ws.registerCallback({type:'Onmessage', requestId, funx:(message)=>{
if(message.id == requestId ) { // same request send if(message.id == requestId ) { // same request send
console.log('message login', message)
if(message.result.token) { if(message.result.token) {
this.isLogin = true this.isLogin = true
@@ -290,9 +286,8 @@ export class WsChatService {
} }
}, },
connect:(url)=> { connect:()=> {
this.wsUrl = url this.socket = new WebSocket(environment.apiWsChatUrl);
this.socket = new WebSocket(this.wsUrl);
// bind function // bind function
this.socket.onopen = this.ws.onopen; this.socket.onopen = this.ws.onopen;
this.socket.onmessage = this.ws.onmessage; this.socket.onmessage = this.ws.onmessage;
@@ -302,6 +297,7 @@ export class WsChatService {
onopen:()=> { onopen:()=> {
this.ws.connected = true this.ws.connected = true
console.log('================== welcome to socket server =====================') console.log('================== welcome to socket server =====================')
this.ws.wsMsgQueue() this.ws.wsMsgQueue()
}, },
wsMsgQueue:()=> { wsMsgQueue:()=> {
@@ -350,7 +346,8 @@ export class WsChatService {
onclose:(event: any)=> { onclose:(event: any)=> {
this.connect(this.firstPingFunx()) this.connect()
this.login()
this.ws.connected = false this.ws.connected = false
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`); console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);