annotate src/goodjava/webserver/ServerSentEvents.java @ 1739:3e9f26404433

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 03 Nov 2022 19:32:03 -0600
parents 9713f7fd50b3
children c9c974817d0c
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;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 public class ServerSentEvents {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 private static class Con {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 final Socket socket;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 final Writer writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 Con(Socket socket) throws IOException {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 this.socket = socket;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 this.writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream(), "UTF-8" ) );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 }
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
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 private static class EventHandler {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 private final List<Con> cons = new ArrayList<Con>();
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 synchronized void add(Con con) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 cons.add(con);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 synchronized void write( String url, String content ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 Iterator<Con> iter = cons.iterator();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 while( iter.hasNext() ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 Con con = iter.next();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 Writer writer = con.writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 try {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 writer.write(content);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 writer.write("\n\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 writer.flush();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 } catch(IOException e) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 iter.remove();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 if( cons.isEmpty() )
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 map.remove(url);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 private static final Map<String,EventHandler> map
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 = Collections.synchronizedMap(new HashMap<String,EventHandler>());
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 static void add(Socket socket,Request request) throws IOException {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 Con con = new Con(socket);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 Writer writer = con.writer;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 writer.write("HTTP/1.1 200 OK\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 writer.write("Access-Control-Allow-Origin: *\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 writer.write("Cache-Control: no-cache\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 writer.write("Content-Type: text/event-stream\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 writer.write("\r\n");
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 writer.flush();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 String url = request.url();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 EventHandler handler;
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 synchronized(map) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 handler = map.get(url);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 if( handler==null ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 handler = new EventHandler();
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 map.put(url,handler);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 handler.add(con);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 public static void write( String url, String content ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 EventHandler handler = map.get(url);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 if( handler != null )
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 handler.write(url,content);
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 public static String toData(String message) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 if( message.endsWith("\n") )
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 message = message.substring( 0, message.length() - 1 );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 return "data: " + message.replace( "\n", "\ndata: " ) + "\n";
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
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 public static void writeMessage( String url, String message ) {
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 write( url, toData(message) );
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 }
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 private ServerSentEvents() {} // never
9713f7fd50b3 server-sent events
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 }