Building a Chat Client

The Visual Basic .NET program in Listing 10.5, ChatClient.vb, uses the Chat web service to interact in an online chat. When you run the program, your screen will display a form similar to that shown in Figure 10.6 that you can use to chat with others. To test the Chat web service, simply run multiple copies of the ChatClient program (you can start the program using the Windows Explorer by double-clicking the program’s executable file within the program’s bin folder). Within each program copy, type in a different name (chat handle) and then switch between the program windows typing and sending messages.

click to expand
Figure 10.6: Using the Chat web service to interact with other remote users

To create the ChatClient.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Web Application. Finally, within the Location field, specify the folder within which you want to store the program, and in the Name field type the program name ChatClient. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text boxes and labels previously shown in Figure 10.6 onto the page. You must also drag a timer onto the application as well.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type localhost/Chat/Service1.asmx?WSDL and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. 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.5.

    Listing 10.5 ChatClient.vb

    start example
    Dim WS As localhost.Service1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _ Ä  System.EventArgs) Handles MyBase.Load   Timer1.Interval = 2000   Timer1.Enabled = True   WS = New localhost.Service1() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ Ä  System.EventArgs) Handles Button1.Click   If TextBox2.Text.Length > 0 Then     Dim Message As String     If (TextBox3.Text.Length = 0) Then       Message = "Unknown: "     Else       Message = TextBox3.Text & ": "     End If     Message = Message & TextBox2.Text     TextBox2.Text = ""     Try       WS.PostMessage(Message)     Catch Ex As Exception       MessageBox.Show(Ex.Message)     End Try   End If End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As _ Ä  System.EventArgs) Handles Timer1.Tick   Dim Messages() As String = Nothing   Try     Timer1.Enabled = False     Messages = WS.GetMessages()     Dim Message As String     TextBox1.Text = ""     If Not Messages Is Nothing Then       For Each Message In Messages          TextBox1.Text = TextBox1.Text & Message & vbCrLf       Next     End If   Catch Ex As Exception     MessageBox.Show(Ex.Message)   End Try   Timer1.Enabled = True End Sub
    end example

The program uses text boxes to display the chat messages, to let the user enter a new message, and to let the user specify a “chat handle” (a name or nickname) the program will use to identify the message sender.

A user sends a message by typing the message text and then clicking the Send Message button. The program uses a timer to control when the program calls the web service to retrieve messages. The timer event occurs every 2 seconds (which should be fast enough for small chats with a limited number of users). Each time the timer event occurs, the program calls the web service’s GetMessages method to retrieve an array of strings that contains the most recent 20 messages. The program then loops through the array, displaying the messages within the text box.

. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa © 2008-2017.
If you may any questions please contact us: