view src/goodjava/webserver/ServerSentEvents.java @ 1744:db52c29605e2

support svg
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 13 Nov 2022 19:26:41 -0700
parents d778f1f2598a
children
line wrap: on
line source

package goodjava.webserver;

import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.Socket;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Collections;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
import goodjava.logging.Logger;
import goodjava.logging.LoggerFactory;


public class ServerSentEvents {
	private static final Logger logger = LoggerFactory.getLogger(ServerSentEvents.class);

	private static class Con {
		final Socket socket;
		final Writer writer;

		Con(Socket socket) throws IOException {
			this.socket = socket;
			this.writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream(), "UTF-8" ) );
		}
	}

	private static class EventHandler {
		private final List<Con> cons = new ArrayList<Con>();

		synchronized void add(Con con) {
			cons.add(con);
		}

		synchronized void write( String url, String content ) {
			Iterator<Con> iter = cons.iterator();
			while( iter.hasNext() ) {
				Con con = iter.next();
				Writer writer = con.writer;
				try {
					writer.write(content);
					writer.write("\n\n");
					writer.flush();
				} catch(IOException e) {
					//logger.info("removing con from "+url);
					iter.remove();
					try {
						con.socket.close();
					} catch(IOException e2) {
						//logger.info("",e2);
					}
				}
			}
			if( cons.isEmpty() ) {
				map.remove(url);
				//logger.info("removed "+url);
			}
		}
	}

	private static final Map<String,EventHandler> map
		= Collections.synchronizedMap(new HashMap<String,EventHandler>());

	static void add(Socket socket,Request request) throws IOException {
		Con con = new Con(socket);

		Writer writer = con.writer;
		writer.write("HTTP/1.1 200 OK\r\n");
		writer.write("Access-Control-Allow-Origin: *\r\n");
		writer.write("Cache-Control: no-cache\r\n");
		writer.write("Content-Type: text/event-stream\r\n");
		writer.write("X-Accel-Buffering: no\r\n");
		writer.write("\r\n");
		writer.flush();

		String url = request.url();
		EventHandler handler;
		synchronized(map) {
			handler = map.get(url);
			if( handler==null ) {
				handler = new EventHandler();
				map.put(url,handler);
			}
		}
		handler.add(con);
	}

	public static void write( String url, String content ) {
		EventHandler handler = map.get(url);
		if( handler != null )
			handler.write(url,content);
	}

	public static String toData(String message) {
		if( message.endsWith("\n") )
			message = message.substring( 0, message.length() - 1 );
		return "data: " + message.replace( "\n", "\ndata: " ) + "\n";
	}

	public static void writeMessage( String url, String message ) {
		write( url, toData(message) );
	}

	private static void sweep() {
		List<String> urls;
		synchronized(map) {
			urls = new ArrayList<String>(map.keySet());
		}
		for( String url : urls ) {
			write( url, "event: ping\n" );
		}
	}

	private static final Timer sweeper = new Timer("ServerSentEvents",true);
	static {
		long period = 1000L*60*60;  // hour
		TimerTask task = new TimerTask() {
			public void run() {
				sweep();
			}
		};
		sweeper.schedule(task,period,period);
	}

	private ServerSentEvents() {}  // never
}