Websockets Client Bug Fixed, that prevented concurrent messages.

This commit is contained in:
Luis Fernando Molina committed 2021-09-16 17:02:44 +02:00
1 parent e351fe271f
commit 44033decc6
7 files changed
+55 -22

No files matched your search

@@ -9,6 +9,7 @@ function newSocialTradingModulesWebSocketsClient() {
let socketClient
let called = {}
let onMessageFunctionsMap = new Map()
return thisObject
@@ -18,6 +19,7 @@ function newSocialTradingModulesWebSocketsClient() {
clientInterface = undefined
called = undefined
onMessageFunctionsMap = undefined
}
async function initialize() {
@@ -38,6 +40,7 @@ function newSocialTradingModulesWebSocketsClient() {
socketClient.onerror = err => { onError(err) }
function onConnection() {
socketClient.onmessage = socketMessage => { onMenssage(socketMessage) }
resolve()
}
@@ -65,22 +68,17 @@ function newSocialTradingModulesWebSocketsClient() {
}
let socketMessage = {
messageId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(),
messageType: 'Request',
payload: message
}
socketClient.onmessage = socketMessage => { onMenssage(socketMessage) }
onMessageFunctionsMap.set(socketMessage.messageId, onMenssageFunction)
socketClient.send(
JSON.stringify(socketMessage)
)
)
function onMenssage(socketMessage) {
function onMenssageFunction(response) {
try {
let response = JSON.parse(socketMessage.data)
/*
Chack if we are waiting for the Handshake response.
*/
if (response.result === 'Ok') {
resolve(response.data)
} else {
@@ -95,4 +93,15 @@ function newSocialTradingModulesWebSocketsClient() {
}
}
}
function onMenssage(socketMessage) {
let response = JSON.parse(socketMessage.data)
/*
We get the functioin that is going to resolve or reject the promise given.
*/
onMenssageFunction = onMessageFunctionsMap.get(response.messageId)
onMessageFunctionsMap.delete(response.messageId)
onMenssageFunction(response)
}
}