My application is a LiveChat developed in C# and ASP.NET
I have a Client timer which call the webservice Function RetrieveMessages() every second. Additionally, soon after the user send a message i'm call de same function RetrieveMessages().
I have a variable that store the last message Id, so, every RetrieveMessage call retrieve only the messages that is unread.
Sometimes the the client show the same message twice, like so:
Ewerton (14:22:20) : Hello! Ewerton (14:22:20) : Hello!
the duplicity occurs only in client, the DataBase table is ok, no duplications.
I suspect tha the Timer, and the Send Message is executing a RetrieveMessage before the variable lastMessageId is updated.
How can i synchronize the call of the RetrieveMessage() ?
Here are some code to analise.
// ThisFunction is a callback that RetrieveMessage every time the user send's a message
function SendMessageSucess(cdMsgEnviada) {
//Carrega Mensagens
Avalon.Services.ChatService.RetrieveMessages(CodChamado, IdLastMsg, RetrieveMessagesSucess);
}
OnTImer Tick
// Every second, verify is exist new messages
function timer_onTick() {
//Carrega Mensagens
Avalon.Services.ChatService.RetrieveMessages(CodChamado, IdLastMsg, RetrieveMessagesSucess);
}
The RetrieveMessages function
function RetrieveMessagesSucess(result) {
var myMsgs = new Array();
for (var i = 0; i < result.length; i++) {
var obj = eval('(' + result[i] + ')');
if (obj != null)
myMsgs[i] = obj;
}
for (var j = 0; j < myMsgs.length; j++) {
if (myMsgs.length > 0) {
// Armazeno o codigo da ultima mensagem recebida
IdLastMsg = myMsgs[myMsgs.length - 1].cd_chat_message;
if (par) {
var novoconteudo = "<div style='background-color: #EFEFEF; padding: 10px;'>"
par = false;
}
else {
var novoconteudo = "<div style='padding: 10px;'>"
par = true;
}
if (myMsgs[j].origem_mensagem == 1) // Msg enviada pelo cliente
novoconteudo = novoconteudo + "<b>" + myMsgs[j].solicitante + ": </b>"
else
novoconteudo = novoconteudo + "<b>" + myMsgs[j].tecnico + ": </b>"
var objDate = eval(myMsgs[j].datahora.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
novoconteudo = novoconteudo + "(" + objDate.format("HH:MM:ss") + ") ";
novoconteudo = novoconteudo + myMsgs[j].texto + "</div>";
divChatHistory.append(novoconteudo);
AutoScroll();
if (myMsgs[j].origem_mensagem == 2) // Msg enviada por um tecnico
show_popAlert()
}
}
// Verifico se o chat esta ativo
IsChatInativo();
}
Any Ideas ?