Running the Sample
Start the service first and it should show you a message saying that the service is running.
Start as many instances of the client as you want and connect to the service. (The UI is intuitive enough i guess !)
Note: The sample is configured to run properly when both the client and the service is hosted on the same machine. Scroll to the end of the article for instructions for setting up the service and client on different machines.
The Service - Console Application
o Contract
->IMyService with three methods.
- SendMessage. - Broadcasts the message sent by the client to all connected clients including itself.(Hence the 'Chat room' effect.)
- Join - Used by the clients to subscribe to the 'Broadcast' event. (Join the chat room)
- Leave - Used by the clients to unsubscribe from the 'Broadcast' event.(Leave the chat room)
->IMyCallback with one method.
RecieveMessage - Implemented by the client and invoked by the service during broadcast.
o Binding
->wsDualHttpBinding ( A dual tcp might perform even better !)
Note: All the methods in the service contract are marked as 'One Way' due to this.
o Event
->Broadcast event is defined and is declared as static so that it is shared by all the clients.
The Client - Windows Forms Application
As stated earlier, the client application must be 'Stateful' and hence a windows aplication has been used. The client class implements the callback interface of the service. It then merely uses the appropriate method of the service based on the user actions.
(You might find more lines of code that does not relate to any of what has been mentioned here and all that is related to UI validations, exception handling and proper port identification for the client)
Hosting the Service and Client on different machines
A few changes have to be made in the configuration file('app.config' in the sample) of the Client application before it can be run from a different machine. To be more precise, TWO changes have to be made.
1. In bindings >> wsDualHttpbinding >> binding - look for an attribute named 'clientBaseAddress'. Replace 'localhost' by the name/ip of the Client machine.
2. In client >> endpoint - look for an attribute named 'address'. Replace 'localhost' by the name/ip of the server.
And you're done...!!!