Interact with Kaazing WebSocket Gateway Using the WebSocket API

This procedure describes how you can use the WebSocket API—provided by the Kaazing Silverlight client library—from a Silverlight application. This API allows you to take advantage of the WebSocket standard as described in the HTML5 specification. For example, you can create a Silverlight application that uses the Kaazing Silverlight client library to interact directly with a back-end server. The support for WebSocket is provided by the WebSocket class and its supporting classes.

The following steps allow you to use the WebSocket API in a Silverlight application. This example highlights some of the most commonly used WebSocket methods and is not meant to be an end-to-end tutorial.

Before You Begin

This procedure is part of Checklist: Build Microsoft .NET and Silverlight Clients Using Kaazing WebSocket Gateway:

  1. Set Up Your Development Environment
  2. Interact with Kaazing WebSocket Gateway Using the WebSocket API
  3. Interact with Kaazing WebSocket Gateway Using the EventSource API
  4. Interact with Kaazing WebSocket Gateway Using the ByteSocket API
  5. Configure Automatic Connection
  6. Secure Your Microsoft .NET and Silverlight Client
  7. Display Logs for .NET and Silverlight Clients
  8. Troubleshoot Your Microsoft .NET and Silverlight Clients

Note: Learn about supported browsers, operating systems, and platform versions in the Release Notes.

To Use the WebSocket API

  1. Add the following required import statement:

    using Kaazing.HTML5;

    Note: The .NET and Silverlight client libraries are signed.

  2. Create a new WebSocket object, as shown in the following example.

    WebSocket mySocket = new WebSocket();

  3. Add event listeners to the WebSocket object. The following example shows how three event listeners are created and then registered with the WebSocket object. The OpenEvent listener is called when a WebSocket connection is established, the MessageEvent listener is called when messages are received, and the CloseEvent listener is called when the WebSocket connection is closed.
    mySocket.OpenEvent += new OpenEventHandler((object sender, OpenEventArgs e) =>
    {
      // Update the UI to show that the application
      //            is now connected.
      // Now that the socket is open, you can use mySocket.Send
      // to send messages.
    });
    
    mySocket.MessageEvent += new MessageEventHandler((object sender,
    MessageEventArgs e) =>
      {
        string messageData = e.Data;
    
        // Use the data and event info to update the UI.
        //            For example:
        this.Dispatcher.BeginInvoke(() =>
        {
          Message.TextBox.text = messageData;
        });
      });
    
    mySocket.CloseEvent += new CloseEventHandler((object sender, CloseEventArgs e) =>
      {
        // Handle errors by closing the application
        //            or attempting to reconnect.
      });
                    

    Note: The Kaazing client libraries fire events on multiple threads for better performance. Therefore, events are delivered on a thread other than the UI thread. This requires you to explicitly transfer execution to the UI thread when updating the UI from dispatched events. This technique varies slightly between Silverlight and .NET Framework applications.

  4. Use the connect method to connect to a back-end service, as shown in the following example. Note that a WebSocket can only connect to one URI at a time.

    mySocket.Connect("ws://localhost:8001/echo");

  5. While the socket is open (that is, after the OpenEvent listener is called and before the CloseEvent listener is called), you can use the Send method to send text-only messages as shown in the following example.

    mySocket.Send("Hello, World");

  6. Later, you can use the close method at any time to deliberately disconnect from the WebSocket server, as shown in the following example.

    mySocket.Close();

Notes

  • The Microsoft .NET 4.0 Framework has a maximum connection limit of two per domain, similar to the browser limitation. For any Microsoft .NET application that uses more than one WebSocket connection at a time, you must either ensure that any WebSocket connection is closed by using WebSocket.Close() before opening another WebSocket connection, or increase the connection limit on the application by updating the maxconnection attribute in the app.config file. For more information, see (KG-1851) Two Connection Limit in Kaazing Clients for Microsoft .NET in Release Notes.
  • You can verify that Kaazing has signed the relevant .NET DLL by selecting the DLL in the File Browser, then right-clicking and opening the Properties dialog. On the Digital Signatures tab, you can view the Name of Signer value "Kaazing Corporation" and a timestamp of when the DLL was signed. The email address is "Not available." For more information, see an example C program that shows how to use the Microsoft mechanism to verify a signature (a DLL is one example of a Portable Executable, or PE, file). You can also learn more about preventing DLL pre-loading attacks.

Next Step

Interact with Kaazing WebSocket Gateway Using the EventSource API

See Also

.NET and Silverlight Client API

TOP