Answer the question
In order to leave comments, you need to log in
Consistency of a global variable with simultaneous requests to sockets?
Simplified code example, clarification of the question below it.
var SUPERGLOBALVARIABLE = {
user228: {
wallet: {
RUB: 0,
EUR: 0,
USD: 0
}
},
user303: {
wallet: {
RUB: 100,
EUR: 0,
USD: 0
}
}
};
io.on('connect,'function(socket) {
//после магии с авторизацией выяснили что ид юзера 303, соответственно у коннекта будет 100 рублей на счету SUPERGLOBALVARIABLE['user' + '303'].wallet.RUB;
//два события: один на обмен евро, другой на обмен долларов. Обменивает он всегда по 1 единице валюты и всегда по заданному глобально курсу.
socket
.on('exchangeEUR', () => {
if (SUPERGLOBALVARIABLE['user' + '303'].wallet.RUB > 70)
SUPERGLOBALVARIABLE['user' + '303'].wallet.RUB -= 70;
SUPERGLOBALVARIABLE['user' + '303'].wallet.EUR += 1;
};
})
.on('exchangeUSD', () => {
if (SUPERGLOBALVARIABLE['user' + '303'].wallet.RUB > 65)
SUPERGLOBALVARIABLE['user' + '303'].wallet.RUB -= 65;
SUPERGLOBALVARIABLE['user' + '303'].wallet.USD += 1;
};
});
});
Answer the question
In order to leave comments, you need to log in
The node executes code in 1 thread, the developer is guaranteed that only 1 code is executed at 1 point in time
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question