Interact with Kaazing WebSocket Gateway Using the ByteSocket API

This procedure describes how you can use the ByteSocket API—provided by the Kaazing Silverlight client library—from a Silverlight application. You use the ByteSocket API in the same way you use the WebSocket API, but ByteSocket allows you to establish a bi-directional socket connection that you can use to send and receive binary data. This section only highlights where the use of WebSocket and ByteSocket differ.

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 ByteSocket API

  1. Create a new ByteSocket object, as shown in the following example.

    ByteSocket mySocket = new ByteSocket();

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

  2. The event listeners for the ByteSocket object must handle binary data as opposed to text data. For example, add a listener that handles a ByteBuffer object, as shown in line 4 in the following example.

    mySocket.MessageEvent += new ByteMessageEventHandler((object sender,
    ByteMessageEventArgs e) =>
      {
        ByteBuffer messageData = e.Data;
    
          // Use the data and event info to update the UI.
          // For example:
          this.Dispatcher.BeginInvoke(() =>
          {
            Message.TextBox.text = messageData.GetString(System.Text.Encoding.UTF8);
          });
      });
                    

    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.

  3. When you send messages, you send binary data using a ByteBuffer object, as shown in the following example. Since you are sending binary data, create a ByteBuffer object in which you store the bytes that you want to send.

    // send a buffer of data.
    ByteBuffer SendBuffer = new ByteBuffer();
    
    // ...
    // Modify the ByteBuffer and fill it with binary data.
    // ...
    
    mySocket.Send(SendBuffer);

Next Step

Configure Automatic Connection

See Also

.NET and Silverlight Client API

TOP