HTML and CSS Reference
Enhancing the WebSocket Server
The solution you have developed so far implements the websocket protocol and demonstrates how data is
passed between the server and client. However, using a websocket server that merely echoes the message back is
not very useful. This server must manage the communication with both clients and agents. When a client sends a
message to the server it will be forwarded to the appropriate agent. When the agent sends a response (back to the
server) the server must route that back to the corresponding client.
To do this you will implement two more classes in the WsServer project:
WsAgentConnection - manages the communication with an agent application
WsClientConnection - manages the communication with a client application
Both of these classes will use an instance of the WsConnection class to send and receive messages. The
WsAgentConnection class will reference up to four instances of the WsClientConnection class that represent
the four clients the agent is currently chatting with. The WsClientConnection must also have a reference to the
WsAgentConnection object that represents the agent supporting this client. This is described in Figure 13-9 .
Figure 13-9. The internal server classes
When the server first receives a connection, it doesn't yet know if it is a client or an agent. It will create a
WsConnection object that will listen for messages. Both applications will be coded to immediately send a message
to the server identifying both the type of application (client or agent) and also the agent's or client's name. When
this message is received, the server will then create either a WsAgentConnection object or a WsClientConnection
object and add this to either the agent or client list.
If this is a client, the server will find an available agent and perform the necessary linkages between the
WsAgentConnection and WsClientConnection objects. The server will also send a response to the client letting
them know the name of the agent that they will be working with. If this is an agent, a WsAgentConnection is
created and added to the agent list so it is available to respond to new clients.
When the WsConnection object is first created, it's MessageReceived and Disconnected events are handled
by the WsServer object. The server will need to process the incoming message that identifies the client or agent.