public class ChirpPublisher
extends java.lang.Object
ChirpPublisher
is used to publish a Chirp service on the local network. You can
construct and start the publisher manually, or use the simpler class builder, like so:
ChirpPublisher publisher = Chirp.publish("com.example.service"). start(getApplication())Optionally, you can also set a payload for the publisher that gets sent along to clients listening for your service. The payload can contain any data you want that can be serialized into a JSON object, as long as it's less than
Chirp.MAX_PAYLOAD_BYTES
(32KB) in size
(after serialization). For example, if you're using to mDNS/Bonjour, you might want to include a
port number for your service in the payload:
Map<String, Object> payload = new HashMap<>(); payload.put("port", 1337); ChirpPublisher publisher Chirp.publish("com.example.service"). payload(payload). start(getApplication());When you no longer want your service published:
publisher.stop();
Modifier and Type | Class and Description |
---|---|
static class |
ChirpPublisher.Builder
Used to chain configuration calls for creating a
ChirpPublisher . |
Constructor and Description |
---|
ChirpPublisher(java.lang.String serviceName)
Create a publisher that can publish the specified service name.
|
Modifier and Type | Method and Description |
---|---|
void |
setPayload(java.util.Map<java.lang.String,java.lang.Object> p)
Sets the payload for this service.
|
void |
setTtl(int ttl)
Sets the ttl of the service in seconds.
|
void |
start(Application app)
Starts the publisher.
|
void |
stop()
Stops the publisher.
|
public ChirpPublisher(@NonNull java.lang.String serviceName)
Chirp.publish(String)
to create, configure and start a ChirpPublisher
instead of using this constructor.serviceName
- a valid service namepublic void setPayload(java.util.Map<java.lang.String,java.lang.Object> p)
IllegalArgumentException
will be thrown.
This method has no effect if called after the publisher has been started.p
- the payloadpublic void setTtl(int ttl)
ttl
- public void start(Application app)
app
- the Application
object is requested instead of a Context
to avoid memory leaks from an Activity
being passed inpublic void stop()
ChirpPublisher
can not be started again after it has been stopped.