HTML and CSS Reference
In-Depth Information
To connect as an agent, the system will want to provide some authentication to ensure only authorized
users can respond to clients. To simulate that, the agent application will use the standard ASP.NET web form
that provides a login capability. You'll then add a custom Chat page that will allow the agent to response to four
simultaneous chat sessions. However, anyone should be able to connect as a client so you'll use the Basic MVC
template that you've used in previous chapters.
Both applications will connect to the websocket server using the normal handshaking protocol. Once
connected, the Agent will send a message to the server that includes their name. This will signal the server that a
new agent has come online. The agent's connection will be saved in a collection for future use.
The client application will also send a message to the server after the connection has been established,
specifying the name of the client. The server will then find the first available agent that has an open chat session
and send a message to that agent, providing the client's name. The agent page will then save the client's name on
the page. At the same time, the server will send a message to the client, letting them know that an agent has been
assigned to them.
At this point both the client and agent can send a message to the server, which is forwarded to the other
application. Since the agent application can have four active sessions, the server will prefix the message with the
client number so the agent application will know which session to update.
If the client disconnects, the server will send a message to the agent letting them know that. The agent
application will then clear the corresponding chat session. If the agent disconnects, all the clients with active
sessions with that agent are also notified and instructed to attempt a re-connect.
Creating a Simple Application
In this section you'll build a websocket server that handles the basic message protocols and test it using a simple
web client. Initially, the server will just echo the message back to the client. You'll later add the functionality
needed by the chat application. For this exercise, the server will be hosted in a console application.
Creating a WebSocket Server
To implement the websocket server you will create a WsServer class. This class creates a socket that it uses to
listen for new connections. When a connection is received, it creates another socket for that connection and
performs the handshaking that I described earlier. If the handshaking is successful it creates an instance of a
WsConnection class that will manage the client connection.
The WsConnection class uses the new socket to listen for incoming messages on that connection. This class
invokes the ReadMessage() method to process an incoming message. This handles all of the frame decoding and
unmasking that may be required. The WsConnection class also provides a SendMessage() method that will send a
message to the client at the other end of the connection.
The WsConnection class provides two events that are raised if handlers are provided. The first event is raised
when an incoming message has been received. The second event is raised when the connection has been closed.
The WsServer class will provide the event handlers for these events.
eXerCISe 13-1. CreatING a SIMpLe WeBSOCKet SerVer
Start Visual Studio 2012 and create a new project named WsServer . Select the
Console Application template from the Windows category. Change the solution name
to Chapter 13 as shown in Figure 13-4 .
Search WWH ::

Custom Search