HTML and CSS Reference
string s=e.Message.Substring(0, 1);
if (int.TryParse(s, out i))
if (client !=null)
public void SendMessage(string msg)
if (_connection !=null)
public void Disconnected(WsConnection sender, EventArgs e)
if (AgentDisconnected != null)
public void Dispose()
if (_connection != null)
The WsAgentConnection class uses a Dictionary to store the client connections. The key will be an integer
(1-4) which will be important to the agent application so it knows which chat window to update. The constructor
creates all four entries, setting the WsConnection reference to null. A null reference indicates that there is no client
actively communicating in this window. The constructor also receives the associated WsConnection object. This
is saved in the _connection member and is used to send a message to the agent application.
When an agent sends a message to the server it is prefixed with a number (1-4) indicating which client this
should be forwarded to. The MessageReceived() event handler strips this off and then finds the corresponding
WsClientConnection object in the Dictionary . The remainder of the message is then forwarded to the client
using the WsClientConnection object's SendMessage() method.
The SendMessage() method simply calls the SendMessage() of the WsConnection object associated with this
agent. The Disconnected() event handler raises the AgentDisconnected event, which will be handled by the
In the Solution Explorer, right-click the WsServer project and select the Add ➤ Class
links. Enter WsClientConnection.cs for the class name.
Enter the code shown in Listing 13-9 as the implementation for this class.