HTML and CSS Reference
When a client sign-on is received, very similar processing is done to create the WsClientConnection object
and re-wire the event handlers. In addition, the code looks for an available agent. This iterates through the
_agents list and for each agent, looks for a Dictionary entry with a null WsClientConnection reference. The
search stops when the first null entry is found.
■ You might want to improve this search to load balance between available agents. As this is currently
implemented, the first agent will handle the first four clients while the other agents are idle. The search could iterate
through the agents looking for the one with the fewest active clients and send the new client to them.
If an available agent was found, the WsAgentConnection object and the Dictionary key are passed to
the WsClientConnection object constructor. Also, the WsClientConnection object is stored in the available
Dictionary entry. A message is sent to agent application letting them know that a new client has been assigned
to them. Finally, a message is sent to the client application letting the client know which agent will be assisting
them. If there are no available agents, a message indicating that is sent to the client application.
Add the event handlers shown in Listing 13-11 to the WsServer class.
Listing 13-11. Adding the additional event handlers
void ClientDisconnected(WsClientConnection sender, EventArgs e)
if (sender._agent != null)
void AgentDisconnected(WsAgentConnection sender, EventArgs e)
foreach (KeyValuePair<int, WsClientConnection>d in sender._clients)
if (d.Value != null)
("The agent has been disconnected; please reconnect");