org.eclipse.jetty.servlets.EventSource Maven / Gradle / Ivy
Show all versions of jetty-servlets Show documentation
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.servlets;
import java.io.IOException;
/**
* {@link EventSource} is the passive half of an event source connection, as defined by the
* EventSource Specification.
* {@link EventSource.Emitter} is the active half of the connection and allows to operate on the connection.
* {@link EventSource} allows applications to be notified of events happening on the connection;
* two events are being notified: the opening of the event source connection, where method
* {@link EventSource#onOpen(Emitter)} is invoked, and the closing of the event source connection,
* where method {@link EventSource#onClose()} is invoked.
*
* @see EventSourceServlet
*/
public interface EventSource
{
/**
* Callback method invoked when an event source connection is opened.
*
* @param emitter the {@link Emitter} instance that allows to operate on the connection
* @throws IOException if the implementation of the method throws such exception
*/
public void onOpen(Emitter emitter) throws IOException;
/**
* Callback method invoked when an event source connection is closed.
*/
public void onClose();
/**
* {@link Emitter} is the active half of an event source connection, and allows applications
* to operate on the connection by sending events, data or comments, or by closing the connection.
* An {@link Emitter} instance will be created for each new event source connection.
* {@link Emitter} instances are fully thread safe and can be used from multiple threads.
*/
public interface Emitter
{
/**
* Sends a named event with data to the client.
* When invoked as: event("foo", "bar")
, the client will receive the lines:
*
* event: foo
* data: bar
*
*
* @param name the event name
* @param data the data to be sent
* @throws IOException if an I/O failure occurred
* @see #data(String)
*/
public void event(String name, String data) throws IOException;
/**
* Sends a default event with data to the client.
* When invoked as: data("baz")
, the client will receive the line:
*
* data: baz
*
* When invoked as: data("foo\r\nbar\rbaz\nbax")
, the client will receive the lines:
*
* data: foo
* data: bar
* data: baz
* data: bax
*
*
* @param data the data to be sent
* @throws IOException if an I/O failure occurred
*/
public void data(String data) throws IOException;
/**
* Sends a comment to the client.
* When invoked as: comment("foo")
, the client will receive the line:
*
* : foo
*
*
* @param comment the comment to send
* @throws IOException if an I/O failure occurred
*/
public void comment(String comment) throws IOException;
/**
* Closes this event source connection.
*/
public void close();
}
}