andrerpena / chatjs

Platform-independent jQuery plugin for chatting
MIT License
184 stars 91 forks source link

Closing chat/pm windows then re-opening #12

Open onsetrese opened 9 years ago

onsetrese commented 9 years ago

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 commented 9 years ago

hi, i have same problem. any suggestions? thanks

onsetrese commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 8 years ago

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

rpaschoal commented 8 years ago

Hello @watoo,

I will share as soon as I get at home!

Cheers!

eniebla commented 8 years ago

Excellent solution @rpaschoal,

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

Muchas Gracias por tu aporte

rpaschoal commented 8 years ago

@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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

@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!