annotate src/goodjava/webserver/ServerSentEvents.java @ 1768:ad53577872cc

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 07 Jun 2023 09:26:10 -0600
parents d778f1f2598a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.webserver;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.Writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.OutputStreamWriter;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.BufferedWriter;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.io.IOException;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.net.Socket;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.util.List;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.ArrayList;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.util.Map;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import java.util.HashMap;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 import java.util.Collections;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 import java.util.Iterator;
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
14 import java.util.Timer;
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
15 import java.util.TimerTask;
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
16 import goodjava.logging.Logger;
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
17 import goodjava.logging.LoggerFactory;
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 public class ServerSentEvents {
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
21 private static final Logger logger = LoggerFactory.getLogger(ServerSentEvents.class);
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 private static class Con {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 final Socket socket;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 final Writer writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 Con(Socket socket) throws IOException {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 this.socket = socket;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 this.writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream(), "UTF-8" ) );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 private static class EventHandler {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 private final List<Con> cons = new ArrayList<Con>();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 synchronized void add(Con con) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 cons.add(con);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 synchronized void write( String url, String content ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 Iterator<Con> iter = cons.iterator();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 while( iter.hasNext() ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 Con con = iter.next();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 Writer writer = con.writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 try {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 writer.write(content);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 writer.write("\n\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 writer.flush();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 } catch(IOException e) {
1741
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
50 //logger.info("removing con from "+url);
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 iter.remove();
1741
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
52 try {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
53 con.socket.close();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
54 } catch(IOException e2) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
55 //logger.info("",e2);
Franklin Schmidt <fschmidt@gmail.com>
parents: 1740
diff changeset
56 }
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 }
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
59 if( cons.isEmpty() ) {
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 map.remove(url);
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
61 //logger.info("removed "+url);
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
62 }
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 private static final Map<String,EventHandler> map
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 = Collections.synchronizedMap(new HashMap<String,EventHandler>());
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 static void add(Socket socket,Request request) throws IOException {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 Con con = new Con(socket);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 Writer writer = con.writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 writer.write("HTTP/1.1 200 OK\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 writer.write("Access-Control-Allow-Origin: *\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 writer.write("Cache-Control: no-cache\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 writer.write("Content-Type: text/event-stream\r\n");
1742
d778f1f2598a make server side events proxy work
Vadim Filimonov <fffilimonov@yandex.ru>
parents: 1741
diff changeset
77 writer.write("X-Accel-Buffering: no\r\n");
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 writer.write("\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 writer.flush();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 String url = request.url();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 EventHandler handler;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 synchronized(map) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 handler = map.get(url);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 if( handler==null ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 handler = new EventHandler();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 map.put(url,handler);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 handler.add(con);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 public static void write( String url, String content ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 EventHandler handler = map.get(url);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 if( handler != null )
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 handler.write(url,content);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99 public static String toData(String message) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 if( message.endsWith("\n") )
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 message = message.substring( 0, message.length() - 1 );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 return "data: " + message.replace( "\n", "\ndata: " ) + "\n";
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 public static void writeMessage( String url, String message ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 write( url, toData(message) );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108
1740
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
109 private static void sweep() {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
110 List<String> urls;
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
111 synchronized(map) {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
112 urls = new ArrayList<String>(map.keySet());
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
113 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
114 for( String url : urls ) {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
115 write( url, "event: ping\n" );
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
116 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
117 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
118
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
119 private static final Timer sweeper = new Timer("ServerSentEvents",true);
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
120 static {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
121 long period = 1000L*60*60; // hour
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
122 TimerTask task = new TimerTask() {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
123 public void run() {
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
124 sweep();
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
125 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
126 };
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
127 sweeper.schedule(task,period,period);
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
128 }
c9c974817d0c add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1738
diff changeset
129
1738
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 private ServerSentEvents() {} // never
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 }