Across the Web, millions of users make extensive use of chat and instant-messaging programs. Normally, as shown in Figure 10.5, such programs make use of a server program that resides between the chat participants to relay messages.
Figure 10.5: Most chat programs rely on a server that sits between the chat participants.
Most chat programs take advantage of network sockets to send and receive messages. In general, the client and server programs listen to the network socket for incoming messages. Each time the server receives a message, the server code simply sends the message down the sockets to each of the recipients.
In this section, you will create the Chat web service that client programs can use to interact in a chat environment. Unlike traditional chat programs within which participants monitor network sockets, the function-call nature of the Chat web service poses some development challenges. You must, for example, control access to the shared buffer to prevent one user from changing the buffer’s contents while a second program is accessing the same data. To send data to others within the chat, a program calls the Chat web service’s PostMessage method:
boolean PostMessage(string Message)
Likewise, to receive messages sent by others, client programs must call the service’s GetMessages method:
To create the Chat web service, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Service. Finally, within the Location field, specify the folder within which you want to store the program and the program name Chat. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the service’s components.
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the program statements in Listing 10.4.
Listing 10.4 Chat Web Service
<WebMethod()> Public Function GetMessages() As String() Dim MessageArray(20) As String Dim I As Integer Application.Lock() For I = 0 To MessageArray.Length - 1 MessageArray(I) = Application("Message" & I) Next Application.UnLock() GetMessages = MessageArray End Function <WebMethod()> Public Function PostMessage(ByVal Message As String) _ Ä As Boolean Dim LastMessage As Integer Application.Lock() LastMessage = Application("LastMessage") If (LastMessage = 0) Then Dim I As Integer For I = 0 To 19 Application("Message" & I) = "" Next End If If (LastMessage <> 19) Then Application("Message" & LastMessage) = Message Application("LastMessage") = LastMessage + 1 Else ScrollMessages() Application("Message" & LastMessage) = Message End If Application.UnLock() PostMessage = True End Function Private Sub ScrollMessages() Dim I As Integer For I = 1 To 19 Application("Message" & I - 1) = Application("Message" & I) Next End Sub
The web service stores the chat messages (up to 20 messages) using the Application object. The code stores the messages within the object using variables named Message0 through Message19. After users specify 20 messages, the web service scrolls the messages by assigning the message stored in Application(“Message1”) to Application(“Message0”), the message in Application(“Message2”) to Application(“Message1”) and so on.
The service’s GetMessage method returns an array of strings that contains the most-recently-received 20 messages. The PostMessage method inserts the messages it receives into the Application object.
To use the Chat web service within a real-world application, you might want to extend the service to require that users log in before they can chat. In addition, because the web service uses the Application object, which retains its contents after the programs stop using the service, the service will return the last 20 messages of the previous chat each time a user connects to the service. For a real-world application, you would want the service to clear the previous messages when users begin a new chat.
As multiple client programs connect to the web service, the Application object becomes a shared data object. To prevent one program from changing the buffer’s contents while others are accessing the data, the code locks and later unlocks the Application object each time the service accesses the data.