As we all know, the callback contract of WCF can be used to implement chat rooms. This articleArticleI will use wf4.0 to implement mySad chat room.First, let's look at the results. Then, let's talk about how to implement and use the advantages of wf4.0. Finally, let's summarize. The interface is simple. Sorry.
Effect:
Start the startachatroom project and startSad chat roomFor example:
Logon:
Main chat interface:
There is a check box on the left of, you can choose to send information to whom. The exit button is displayed in the upper right corner. Click I want to leave to close your chat room client.
QilinSending information:
BeautyReceived information, such:
The demonstration is here, and the following describes how to implement it.
Implementation:
This article uses the WCF Service in wf4.0 to implement duplex communication. The Duplex Communication in wf4.0 is different from that in WCF. For details, see the following two articles:
Workflow 4 services and duplex communications)
More workflow 4 services and duplex communications)
Speaking of this, you should be very clear about how it is implemented. Yes, we still use the WCF technology. However, I still want to explain how to implement it with wf4.0, which can also deepen your understanding of WCF and WF and the differences between them.
It's mine.CodeProject Structure:
ChatroomserviceIs a WCF workflow service application project
Chatroom_clientIs a Windows Form Project
StartachatroomYesconsole ApplicationProgram.
Next I will talk about the core logic. For other parts, see the code.
I suppose you and I have a little understanding of WCF, just like you. Otherwise, it is difficult to proceed. :)
See:
We can see the logic of the sad chat room. It has six methods:
1. Open a chat room.
2. Join the chat room.
3. Leave the chat room.
4. Send the message to everyone.
5. Send a message to someone.
6. obtain all the staff in this chat room.
The implementation part of wf4.0 duplex communication is in the customer segment.
OnSad chat roomThe code for enabling a WCF Service for each customer segment is as follows:
Random RO =NewRandom ();IntPoint = Ro. Next (8000,999 9); chatroomcallback =NewChatroomcallback (); Address ="Http: // localhost:"+ Point +"/Servicecallback"; Var servicecallback =NewChatroomcallback (); servicecallback. mainfrm =This; Back. uisynccontext = synchronizationcontext. Current; var host =NewServicehost (servicecallback,NewUri (Address); host. open (); you can see the code above. I pass the address of the customer segment's WCF Service as a parameter to the server. If you know a little about WF, you can use the send activity to call the WCF Service.
Double-click "join chat room" to view the following:
As you can see, all my articles provide code downloads. Read the Code directly. :)
Wf4.0 advantages:Visual Programming, intuitive, simple, no code or a small amount of code during design.
Summary:A simple chat room is implemented using wf4.0. I think this demo can be expanded. If you have any suggestions or questions, please leave a message. Thank you. You are welcome to join the wf4.0 technology exchange group in the blog Park.
Code: http://files.cnblogs.com/zhuqil/ChatRoomService.rar