From e2f69a67882870ac285b310070fb5f73c53fe961 Mon Sep 17 00:00:00 2001 From: Peter Maquiran Date: Fri, 7 Jan 2022 16:34:02 +0100 Subject: [PATCH] Imrpove websocket inicial connect --- src/app/home/home.page.ts | 85 ++----------------- .../socket/rocket-chat-client.service.ts | 16 ++-- 2 files changed, 16 insertions(+), 85 deletions(-) diff --git a/src/app/home/home.page.ts b/src/app/home/home.page.ts index 242e597b3..a56568a49 100644 --- a/src/app/home/home.page.ts +++ b/src/app/home/home.page.ts @@ -98,87 +98,12 @@ export class HomePage implements OnInit { private sqliteservice: SqliteService, private RouteService: RouteService) { - this.RocketChatClientService.connect() - this.RocketChatClientService.login({ - username: 'paulo.pinto@gabinetedigital.local', - password: 'tabteste@006' + this.RocketChatClientService.connect(()=>{ + this.RocketChatClientService.login({ + username: 'paulo.pinto@gabinetedigital.local', + password: 'tabteste@006' + }) }) - - // const url = 'wss://gabinetedigitalchat.dyndns.info/websocket' - // const socket = new WebSocket(url); - // socket.onopen = ()=> { - - // console.log('============================ welcome to rocket chat ========================================='); - - // const connectRequest = { - // msg: "connect", - // version: "1", - // support: ["1", "pre2", "pre1"] - // } - - // // ======================= connect - // socket.send(JSON.stringify(connectRequest )) - // }; - - // socket.onmessage = (event: any)=> { - - // console.log('event.data', JSON.parse(event.data)) - - // const data = JSON.parse(event.data) - - // if(event.data=='{"msg":"ping"}') { - // socket.send('{"msg":"pong"}') - - // const loginRequest = { - // msg: "method", - // method: "login", - // id: uuidv4(), - // params: [ - // { - // "user": { - // "username": "paulo.pinto" - // }, - // "password": "tabteste@006" - // } - // ] - // } - - // // ======================== login - // socket.send(JSON.stringify(loginRequest)) - // } - - // // login successfully - // if(data.result) { - // if(data.result.token) { - // console.log('login successfully') - - // const requestId = uuidv4() - - // var requestMessage = { - // msg: "method", - // method: "sendMessage", - // id: requestId, - // params: [{ - // _id: "message-id" || uuidv4(), - // rid: "room-id" || 444, - // msg: "Hello World!" - // }] - // } - - // // socket.send(JSON.stringify(requestMessage )) - - // } - // } - // }; - - // socket.onclose = (event: any)=> { - // console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`); - // }; - - // socket.onerror =(event: any)=> { - // console.log(`[error] ${event.message}`); - // }; - // this.RocketChatClientService.send() /* this.webNotificationPopupService.askNotificationPermission() */ diff --git a/src/app/services/socket/rocket-chat-client.service.ts b/src/app/services/socket/rocket-chat-client.service.ts index bbac211a6..94f279628 100644 --- a/src/app/services/socket/rocket-chat-client.service.ts +++ b/src/app/services/socket/rocket-chat-client.service.ts @@ -4,14 +4,15 @@ import { v4 as uuidv4 } from 'uuid' class _RocketChatClientService { connected = false + hasPing = false constructor() {} private returns() {} - connect(url = 'wss://gabinetedigitalchat.dyndns.info/websocket') { + connect(firstPing: Function) { - this.ws.connect(url); + this.ws.connect('wss://gabinetedigitalchat.dyndns.info/websocket'); const connectMessage = { msg: "connect", @@ -20,10 +21,15 @@ class _RocketChatClientService { } this.ws.send(connectMessage) - + this.ws.registerCallback('Onmessage',(message: any) => { if(message.msg == "ping") { this.ws.send({msg:"pong"}) + if(this.hasPing == false) { + // first ping + firstPing() + this.hasPing = true + } } }) @@ -51,7 +57,7 @@ class _RocketChatClientService { return requestId } - logout(){ } + logout(){} send(roomId, message, option) { const requestId = uuidv4() @@ -95,7 +101,7 @@ class _RocketChatClientService { } - // socket ================================================================== + // socket class ================================================================== private socket!: WebSocket; private url = '' private callBacks: {