com.streamhub.api
Class JsonPayload

java.lang.Object
  extended by com.streamhub.api.JsonPayload
All Implemented Interfaces:
Payload

public class JsonPayload
extends Object
implements Payload

JsonPayload sends its message as a JSON string. Every message has at least one field 'topic' representing the topic that this message concerns.

Use the following to access the topic of a JsonPayload:

Payload payload = new JsonPayload("GOOG");
String topic = payload.getFields().get("topic");


Constructor Summary
JsonPayload(String topic)
          Creates a new JsonPayload with topic.
 
Method Summary
 void addField(String key, String value)
          Adds a field to the message with the key and value.
 Map<String,String> getFields()
          Returns a map of every single field added to this message
 void timestamp()
          If timestamping is enabled, adds a field named 'timestamp' to the Payload with a value of System.currentTimeMillis().
 byte[] toCometBytes()
          Returns a byte array representing the bytes ready to be sent over the wire to a Comet client.
 void toggleTimestamping(boolean onOrOff)
          Toggles timestamping on or off.
 String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

JsonPayload

public JsonPayload(String topic)
Creates a new JsonPayload with topic. This adds a field to the JSON message on the wire with a key of 'topic' and a value of the parameter topic

Parameters:
topic - the topic this payload will be sent with
Method Detail

addField

public void addField(String key,
                     String value)
Description copied from interface: Payload
Adds a field to the message with the key and value.

Specified by:
addField in interface Payload
Parameters:
key - the key of the message - must be unique
value - the value of the message

getFields

public Map<String,String> getFields()
Description copied from interface: Payload
Returns a map of every single field added to this message

Specified by:
getFields in interface Payload
Returns:
A map of all the fields in this payload

toCometBytes

public byte[] toCometBytes()
Description copied from interface: Payload
Returns a byte array representing the bytes ready to be sent over the wire to a Comet client.

This method should not be used to send a payload, use NIOServer.publish(String, Payload) or Client.send(String, Payload) instead.

Specified by:
toCometBytes in interface Payload
Returns:
A byte array representing the bytes ready to be sent over the wire to a Comet client

timestamp

public void timestamp()
Description copied from interface: Payload
If timestamping is enabled, adds a field named 'timestamp' to the Payload with a value of System.currentTimeMillis().

Specified by:
timestamp in interface Payload
See Also:
Payload.toggleTimestamping(boolean)

toggleTimestamping

public void toggleTimestamping(boolean onOrOff)
Description copied from interface: Payload
Toggles timestamping on or off. By default timestamping is off.

Specified by:
toggleTimestamping in interface Payload
Parameters:
onOrOff - A boolean representing whether to turn on timestamping or not. Use true to enable it, and false to disable it.
See Also:
Payload.timestamp()

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2009 StreamHub. Tutorials, news and guides can be found on the StreamHub Comet Blog.