diff --git a/src/app/services/socket/rocket-chat-client.service.ts b/src/app/services/socket/rocket-chat-client.service.ts index efee7c950..060f13333 100644 --- a/src/app/services/socket/rocket-chat-client.service.ts +++ b/src/app/services/socket/rocket-chat-client.service.ts @@ -7,11 +7,27 @@ class _RocketChatClientService { private hasPing = false private firstPingFunx: Function isLogin = false; - request = [] + requestQueue = [] constructor() {} private returns() {} + + private attemptRequestQueue(arg, option) { + + const conditionToSend = this.ws.connected == true && this.isLogin == true + + if(conditionToSend) { + this.requestQueue.push({ + arg: arg, + status: { + isLogin: this.isLogin, + isConnected: this.ws.connected + } + }) + } + return conditionToSend + } connect(firstPingFunx: Function) { this.hasPing = false @@ -45,6 +61,7 @@ class _RocketChatClientService { login(user) { const requestId = uuidv4() + this.isLogin = false const loginRequest = { msg: "method", @@ -66,6 +83,7 @@ class _RocketChatClientService { if(message.result) { if(message.result.token) { this.isLogin = true + this.ws.wsMsgQueue() resolve(message) } else { this.isLogin = false @@ -84,6 +102,10 @@ class _RocketChatClientService { logout(){} send(roomId, message) { + + const conditions = this.attemptRequestQueue({...roomId, message}, {methodName:'send'}) + if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); } + const requestId = uuidv4() var request = { @@ -134,6 +156,10 @@ class _RocketChatClientService { subscribe(roomId : string) { + + const conditions = this.attemptRequestQueue({roomId},{methodName: 'subscribe'}) + if(conditions == false) { return new Promise((resolve, reject) => { reject('no error')}); } + const requestId = uuidv4() var subscribeRequest = { "msg": "sub", @@ -196,12 +222,16 @@ class _RocketChatClientService { onopen:()=> { this.ws.connected = true console.log('================== welcome to socket server =====================') - this.wsMsgQueue.forEach((item, index, object) => { this.ws.send(item.message, item.requestId); object.splice(index, 1); }) - + }, + wsMsgQueue:()=> { + // this.wsMsgQueue.forEach((item, index, object) => { + // this.ws.send(item.message, item.requestId); + // object.splice(index, 1); + // }) }, send: (message: object, requestId = uuidv4()) => {