alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (EventSource.java)

This example Play Framework source code file (EventSource.java) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

chunks, event, eventsource, exception, lib, library, nullpointerexception, override, play, play framework, string, throwable, whenconnectedeventsource

The EventSource.java Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.libs;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.StringEscapeUtils;
import play.mvc.Results.*;

/**
 * Implementation of Server-Sent Events.
 * @see <a href="http://dev.w3.org/html5/eventsource/">Server-Sent Events specification</a>
 */
public abstract class EventSource extends Chunks<String> {
    private Chunks.Out<String> out;

    /**
     * Create a new EventSource socket
     *
     */
    public EventSource() {
        super(play.core.j.JavaResults.writeString("text/event-stream", play.api.mvc.Codec.javaSupported("utf-8")));
    }

    public void onReady(Chunks.Out<String> out) {
        this.out = out;
        onConnected();
    }

    /**
     * Send an event. On the client, a 'message' event listener can be setup to listen to this event.
     *
     * @param event Event content
     */
    public void send(Event event) {
        out.write(event.formatted());
    }

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

    /**
     * Add a callback to be notified when the client has disconnected.
     */
    public void onDisconnected(F.Callback0 callback) {
        out.onDisconnected(callback);
    }

    /**
     * Close the channel
     */
    public void close() {
        out.close();
    }

    /**
     * Creates an EventSource. The abstract {@code onConnected} method is
     * implemented using the specified {@code F.Callback<EventSource>} and
     * is invoked with {@code EventSource.this}.
     *
     * @param callback the callback used to implement onConnected
     * @return a new EventSource
     * @throws NullPointerException if the specified callback is null
     */
    public static EventSource whenConnected(F.Callback<EventSource> callback) {
        return new WhenConnectedEventSource(callback);
    }

    /**
     * An extension of EventSource that obtains its onConnected from
     * the specified {@code F.Callback<EventSource>}.
     */
    static final class WhenConnectedEventSource extends EventSource {

        private final F.Callback<EventSource> callback;

        WhenConnectedEventSource(F.Callback<EventSource> callback) {
            super();
            if (callback == null) throw new NullPointerException("EventSource onConnected callback cannot be null");
            this.callback = callback;
        }

        @Override
        public void onConnected() {
            try {
                callback.invoke(this);
            } catch (Throwable e) {
                play.PlayInternal.logger().error("Exception in EventSource.onConnected", e);
            }
        }
    }

    /**
     * Utility class to build events.
     */
    public static class Event {

        private final String name;
        private final String id;
        private final String data;

        public Event(String data, String id, String name) {
            this.name = name;
            this.id = id;
            this.data = data;
        }

        /**
         * @param name Event name
         * @return A copy of this event, with name {@code name}
         */
        public Event withName(String name) {
            return new Event(this.data, this.id, name);
        }

        /**
         * @param id Event id
         * @return A copy of this event, with id {@code id}.
         */
        public Event withId(String id) {
            return new Event(this.data, id, this.name);
        }

        /**
         * @return This event formatted according to the EventSource protocol.
         */
        public String formatted() {
            return new play.api.libs.EventSource.Event(data, Scala.Option(id), Scala.Option(name)).formatted();
        }

        /**
         * @param data Event content
         * @return An event with {@code data} as content
         */
        public static Event event(String data) {
            return new Event(data, null, null);
        }

        /**
         * @param json Json value to use
         * @return An event with a string representation of {@code json} as content
         */
        public static Event event(JsonNode json) {
            return new Event(Json.stringify(json), null, null);
        }

    }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework EventSource.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.