HTML and CSS Reference
In-Depth Information
{
string s=e.Message.Substring(0, 1);
int i=0;
if (int.TryParse(s, out i))
{
WsClientConnection client=_clients[i];
if (client !=null)
{
client.SendMessage(e.Message.Substring(2));
}
}
}
public void SendMessage(string msg)
{
if (_connection !=null)
_connection.SendMessage(msg);
}
public void Disconnected(WsConnection sender, EventArgs e)
{
if (AgentDisconnected != null)
AgentDisconnected(this, EventArgs.Empty);
}
public void Dispose()
{
if (_connection != null)
_connection.Dispose();
}
}
}
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
WsServer class.
3.
In the Solution Explorer, right-click the WsServer project and select the Add ➤ Class
links. Enter WsClientConnection.cs for the class name.
4.
Enter the code shown in Listing 13-9 as the implementation for this class.
 
Search WWH ::




Custom Search