Skip to content
This repository has been archived by the owner on Apr 30, 2018. It is now read-only.

Closing chat/pm windows then re-opening #12

Open
onsetrese opened this issue Mar 5, 2015 · 15 comments
Open

Closing chat/pm windows then re-opening #12

onsetrese opened this issue Mar 5, 2015 · 15 comments

Comments

@onsetrese
Copy link

When you open then close a chatbox then reopen the same window( userClicked function ), and type in the textbox and press send, the messages get double send. the client adapter onMessagesChanged function/listener gets called in as many times as you close open the private chat box with the same user. I hope you understand what Im saying :)

@andresarcila
Copy link

hi, i have same problem. any suggestions? thanks

@onsetrese
Copy link
Author

you have to edit this DemoClientAdapter.prototype.onMessagesChanged function. What I did was to also pass the "otherUserId" and have messagesChangedHandlers[otherUserId] = handler, this way, messagesChangedHandlers doesnt grow any longer than its needs to. the handler just gets updated every time a new pm window is opened

@rpaschoal
Copy link
Contributor

Hello onsetrese,

I have the same problem, and the demo on the website too. Can you post the new code with the adjustment that you did.

Thanks!

@rpaschoal
Copy link
Contributor

Can please someone tell me how to do what onsetrese suggested? I'm not being able to put another parameter on the handler like he said.

@onsetrese
Copy link
Author

File: jquery.chatjs.adapter.demo.js
DemoClientAdapter.prototype.onMessagesChanged = function (handler, otherUserId) {
this.messagesChangedHandlers[otherUserId] = handler;
};

File: jquery.chatjs.messageboard.js
find this.options.adapter.client.onMessagesChanged then add the 2nd parameter this.options.otherUserId at the end, basically youll have
this.options.adapter.client.onMessagesChanged(function (message) {...}, this.options.otherUserId);

Hope this helps

@rpaschoal
Copy link
Contributor

Hello,

First of all, thanks for the help! I managed to achieve this a few hours ago, but this won't help me, because my IDs are very random.

Example: "2, 60, 150, 8590".

If I do like you propose, the array gets too big. I need to find another way... Every window register the same events, over and over and over...

It should unregister on the event "onClose", but I still don't know how to do it yet.

@rpaschoal
Copy link
Contributor

I think I found a better solution, and more performatic to the client side (Maybe the author could incorporate this to the project)

I created an event that unregister the handlers. Check out:

*** At the adapter JS FILE:

function SignalRClientAdapter(chatHubClient) {
var _this = this;
this.messagesChangedHandlers = [];
this.typingSignalReceivedHandlers = [];
this.userListChangedHandlers = [];
this.roomListChangedHandlers = [];
this.hubClient = chatHubClient;
this.handlersIndex = -1;
..... THE REST OF INIT CONTINUES ...

// adds a handler to the messagesChanged event
SignalRClientAdapter.prototype.onMessagesChanged = function (handler) {
this.handlersIndex = this.messagesChangedHandlers.length;
this.messagesChangedHandlers.push(handler);
};

// Ao fechar a janela de Private Message, recebe uma notificação para remover todos os handlers desta PM.
SignalRClientAdapter.prototype.onPMClose = function () {
if (this.handlersIndex >= 0) {
console.log("Removendo Handlers da PM. Indice => " + this.handlersIndex);
this.messagesChangedHandlers.splice(this.handlersIndex, 1);
}
};

*** At chatjs.controller.js:

chatPmOptions.onClose = function () {
for (var i = 0; i < _this.pmWindows.length; i++)
if (_this.pmWindows[i].otherUserId == otherUserId) {
this.adapter.client.onPMClose(); // Remove todos os eventos registrados
_this.pmWindows.splice(i, 1);
_this.saveState();
_this.organizePmWindows();
break;
}
};

@irfan-yusanif
Copy link

hi @rpaschoal
It seems you have implemented using signalR. Can you please share a sample project using chatjs with signalR

@rpaschoal
Copy link
Contributor

Hello @Watoo,

I will share as soon as I get at home!

Cheers!

@eniebla
Copy link

eniebla commented Dec 1, 2015

Excellent solution @rpaschoal,

its simple, but works, the project isnt updating since a year

Muchas Gracias por tu aporte

@rpaschoal
Copy link
Contributor

@eniebla you are welcome!

I am glad this helped you out!

This is major bug in current project, author should change that ASAP!

The plugin is awesome, should be more popular.

Cheers!

@eniebla
Copy link

eniebla commented Dec 4, 2015

Hi @rpaschoal , there is the same error, but its now in
this.handlersIndex = this.messagesChangedHandlers.length;
open the first pm window, then open a second pw window,
later close the first that you open, onPMClose() removes the handler of the the second pw window instead of the first, I still don't know how to fix that error.

I tell you later how it's going, sorry for my english i'm learning and i'm new in development

@eniebla
Copy link

eniebla commented Dec 4, 2015

I forget to tell you If you open a third one, the messages get double send, and the second one don't receive the removeTypingSignal

@eniebla
Copy link

eniebla commented Dec 4, 2015

I think i find the final solution for this bug, it's simple. I mix @rpaschoal and @onsetrese ideas, jejeje
FIRST:--->
File: jquery.chatjs.messageboard.js
find this.options.adapter.client.onMessagesChanged, then add the 2nd parameter this.options.otherUserId at the end, basically youll have
this.options.adapter.client.onMessagesChanged(function (message) {...}, this.options.otherUserId);

SECOND:--->
File: jquery.chatjs.adapter.demo.js
function DemoClientAdapter() {
this.messagesChangedHandlers = [];
this.typingSignalReceivedHandlers = [];
this.userListChangedHandlers = [];
this.handlersIndex = new Array(); //this is the fix
}

THIRD AND FINALLY:---->
File: jquery.chatjs.adapter.demo.js
DemoClientAdapter.prototype.onMessagesChanged = function (handler,otherUserId) {
var shouldAddHandler=true;
if ('undefined'!==typeof otherUserId)
for( var i=0; i < this.handlersIndex.length; i++ )
if(this.handlersIndex[i]['id']===otherUserId){
this.messagesChangedHandlers[i]=handler;
shouldAddHandler=false;break;
}
if(shouldAddHandler){
this.handlersIndex.push({id:otherUserId});
this.messagesChangedHandlers.push(handler);
console.log("Adding Handlers PM. Index => " + (this.handlersIndex.length-1) +' for otherUserId=> ' + otherUserId);
}
};

Hope this helps, the author really should change that ASAP!

@rpaschoal
Copy link
Contributor

@eniebla,

Thank you for your response!

I actually changed this in my project as well.... I have a "bug fixed" typescript project up and running!

@andrerpena, are you alive?

Cheers!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants