play.libs
Class EventSource

java.lang.Object
  extended by play.mvc.Results.Chunks<java.lang.String>
      extended by play.libs.EventSource

public abstract class EventSource
extends Results.Chunks<java.lang.String>

Implementation of Server-Sent Events.

See Also:
Server-Sent Events specification

Nested Class Summary
static class EventSource.Event
          Utility class to build events.
 
Nested classes/interfaces inherited from class play.mvc.Results.Chunks
Results.Chunks.Out<A>
 
Constructor Summary
EventSource()
          Create a new EventSource socket
 
Method Summary
 void close()
          Close the channel
abstract  void onConnected()
          The socket is ready, you can start sending messages.
 void onDisconnected(F.Callback0 callback)
          Add a callback to be notified when the client has disconnected.
 void onReady(Results.Chunks.Out<java.lang.String> out)
          Called when the Chunked stream is ready.
 void send(EventSource.Event event)
          Send an event.
 void sendData(java.lang.String data)
          Deprecated. Replaced by send
 void sendDataById(java.lang.String eventId, java.lang.String data)
          Deprecated. Replaced by send
 void sendDataByName(java.lang.String eventName, java.lang.String data)
          Deprecated. Replaced by send
static EventSource whenConnected(F.Callback<EventSource> callback)
          Creates an EventSource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EventSource

public EventSource()
Create a new EventSource socket

Method Detail

onReady

public void onReady(Results.Chunks.Out<java.lang.String> out)
Description copied from class: Results.Chunks
Called when the Chunked stream is ready.

Specified by:
onReady in class Results.Chunks<java.lang.String>
Parameters:
out - The out stream.

sendDataByName

@Deprecated
public void sendDataByName(java.lang.String eventName,
                                      java.lang.String data)
Deprecated. Replaced by send

A single event source can generate different types events by including an event name. On the client, an event listener can be setup to listen to that particular event.

Parameters:
eventName - Unique name of the event.
data - data associated with event
See Also:
send(play.libs.EventSource.Event)

sendDataById

@Deprecated
public void sendDataById(java.lang.String eventId,
                                    java.lang.String data)
Deprecated. Replaced by send

Setting an ID lets the browser keep track of the last event fired so that if, the connection to the server is dropped, a special HTTP header (Last-Event-ID) is set with the new request. This lets the browser determine which event is appropriate to fire.

Parameters:
eventId - Unique event id.
data - data associated with event
See Also:
send(play.libs.EventSource.Event)

sendData

@Deprecated
public void sendData(java.lang.String data)
Deprecated. Replaced by send

Sending a generic event. On the client, 'message' event listener can be setup to listen to this event.

Parameters:
data - data associated with event
See Also:
send(play.libs.EventSource.Event)

send

public void send(EventSource.Event event)
Send an event. On the client, a 'message' event listener can be setup to listen to this event.

Parameters:
event - Event content

onConnected

public abstract void onConnected()
The socket is ready, you can start sending messages.


onDisconnected

public void onDisconnected(F.Callback0 callback)
Add a callback to be notified when the client has disconnected.


close

public void close()
Close the channel


whenConnected

public static EventSource whenConnected(F.Callback<EventSource> callback)
Creates an EventSource. The abstract onConnected method is implemented using the specified F.Callback<EventSource> and is invoked with EventSource.this.

Parameters:
callback - the callback used to implement onConnected
Returns:
a new EventSource
Throws:
java.lang.NullPointerException - if the specified callback is null