Creating separate handlers for different users - WCF chat
I'm following this brilliant piece of article to dissect and understand chat using WCF. The logic is that when a user joins a chat an event handler is created for that particular user and is stored in a dictionary.
lock (syncObj)
{
if (!checkIfPersonExists(person.Name) && person != null)
{
this.person = person;
开发者_如何学Go chatters.Add(person, MyEventHandler);
userAdded = true;
}
}
So multiple handlers are stored for multiple users in the service.
My question is this -
How does the service identifies the target user when a message is sent just by with the help of the event handler? I dont understand the uniqueness of the event handlers that is stored for the users. How does
handler.BeginInvoke(this, e, new AsyncCallback(EndAsync),
null);
invokes the function in the client side that has implemented the duplex client contract interface for that specific users? Which makes them connected? Sessions?
Note: I know that this may be tough to be understood without going through that article. I've tried my best to ask it in a generic way.
Regards
NLV
I think you are asking, how a service responds to a client in a pub/sub service. If so, the answer is that the service uses the callbackchannel OperationContext.Current.GetCallbackChannel<IYourServiceContract>()
that is supplied when the client "Subscribes". If you delve into this callbackChannel you will see that it maintains all of the necessary data to communicate with the client. If you notice, in the sample application you linked, the author is getting this from the person object, b/c he wants to store it in the chatters Array.
callback =
OperationContext.Current.GetCallbackChannel<IChatCallback>();
Here is an article I wrote that might simplify the pub/sub model a bit for you. http://www.codeproject.com/KB/WCF/wcfesb.aspx. It is not a chat program, but might explain a bit more about how communication is achieved through the callback channel.
If this is not what you are asking... please clarify.
精彩评论