Interact with Kaazing WebSocket Gateway Using the EventSource API
This procedure describes how you can use the EventSource API provided by the Kaazing Flash client library. This API allows you to take advantage of the Server-Sent Events standard as described in the HTML5 specification. For example, you can create a Flash client that uses the Kaazing Flash client library to receive streaming data from a news feed for streaming financial data. The support for Server-Sent Events is provided by the EventSource class and its supporting classes.
The following steps show you how to use the EventSource API. This example highlights some of the most commonly used EventSource methods and is not meant to be an end-to-end tutorial. Refer to the ActionScript EventSource API documentation for a complete description of all the available methods.
Before You Begin
This procedure is part of Checklist: Build Flash Clients Using Kaazing WebSocket Gateway:
- Set Up Your Development Environment
- Interact with Kaazing WebSocket Gateway Using the WebSocket API
- Interact with Kaazing WebSocket Gateway Using the EventSource API
- Secure Your Flash Client
- Display Logs for the Flash Client
- Troubleshoot Your Flash Client
Note: Learn about supported browsers, operating systems, and platform versions in the Release Notes.
To Use the EventSource API
- Add the necessary import statements:
import com.kaazing.gateway.client.html5.MessageEvent import com.kaazing.gateway.client.html5.EventSource
- Create a new EventSource object:
var es:EventSource = new EventSource(location.text)
- Add event handlers to the EventSource object to listen for EventSource events, as shown in the following example. The EventSource object provides mechanisms to attach event-handlers that are invoked when the corresponding event is raised. The EventSource object provides support for registering event handlers for specific events. The onopen method is called when a Server-Sent Events connection is established, the onmessage method is called when messages are received, and the onerror method is called when errors occur.
eventSource.onopen = openHandler eventSource.onmessage = readHandler eventSource.onError = errorHandler
- Define the three functions that serve as event handlers:
private function openHandler(ev:Event):void { trace("CONNECTED\n"); } private function readHandler(ev:Event):void { var msg:String = ev.data trace("MESSAGE: "+msg+"\n"); } private function errorHandler(ev:Event):void { trace("ERROR\n"); }
Next Step
Notes
- To view sample source code using the EventSource API, see the sse.mxml file located in GATEWAY_HOME/demo/flash/src/gateway/.