Mercurial Hosting > luan
annotate src/goodjava/webserver/ServerSentEvents.java @ 1831:8f9ae295bf6a default tip
add Hosted.authorize
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 26 Sep 2024 15:07:45 -0600 |
parents | d778f1f2598a |
children |
rev | line source |
---|---|
1738 | 1 package goodjava.webserver; |
2 | |
3 import java.io.Writer; | |
4 import java.io.OutputStreamWriter; | |
5 import java.io.BufferedWriter; | |
6 import java.io.IOException; | |
7 import java.net.Socket; | |
8 import java.util.List; | |
9 import java.util.ArrayList; | |
10 import java.util.Map; | |
11 import java.util.HashMap; | |
12 import java.util.Collections; | |
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 | 18 |
19 | |
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 | 22 |
23 private static class Con { | |
24 final Socket socket; | |
25 final Writer writer; | |
26 | |
27 Con(Socket socket) throws IOException { | |
28 this.socket = socket; | |
29 this.writer = new BufferedWriter( new OutputStreamWriter( socket.getOutputStream(), "UTF-8" ) ); | |
30 } | |
31 } | |
32 | |
33 private static class EventHandler { | |
34 private final List<Con> cons = new ArrayList<Con>(); | |
35 | |
36 synchronized void add(Con con) { | |
37 cons.add(con); | |
38 } | |
39 | |
40 synchronized void write( String url, String content ) { | |
41 Iterator<Con> iter = cons.iterator(); | |
42 while( iter.hasNext() ) { | |
43 Con con = iter.next(); | |
44 Writer writer = con.writer; | |
45 try { | |
46 writer.write(content); | |
47 writer.write("\n\n"); | |
48 writer.flush(); | |
49 } catch(IOException e) { | |
1741 | 50 //logger.info("removing con from "+url); |
1738 | 51 iter.remove(); |
1741 | 52 try { |
53 con.socket.close(); | |
54 } catch(IOException e2) { | |
55 //logger.info("",e2); | |
56 } | |
1738 | 57 } |
58 } | |
1740
c9c974817d0c
add ServerSentEvents.sweep
Franklin Schmidt <fschmidt@gmail.com>
parents:
1738
diff
changeset
|
59 if( cons.isEmpty() ) { |
1738 | 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 | 63 } |
64 } | |
65 | |
66 private static final Map<String,EventHandler> map | |
67 = Collections.synchronizedMap(new HashMap<String,EventHandler>()); | |
68 | |
69 static void add(Socket socket,Request request) throws IOException { | |
70 Con con = new Con(socket); | |
71 | |
72 Writer writer = con.writer; | |
73 writer.write("HTTP/1.1 200 OK\r\n"); | |
74 writer.write("Access-Control-Allow-Origin: *\r\n"); | |
75 writer.write("Cache-Control: no-cache\r\n"); | |
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 | 78 writer.write("\r\n"); |
79 writer.flush(); | |
80 | |
81 String url = request.url(); | |
82 EventHandler handler; | |
83 synchronized(map) { | |
84 handler = map.get(url); | |
85 if( handler==null ) { | |
86 handler = new EventHandler(); | |
87 map.put(url,handler); | |
88 } | |
89 } | |
90 handler.add(con); | |
91 } | |
92 | |
93 public static void write( String url, String content ) { | |
94 EventHandler handler = map.get(url); | |
95 if( handler != null ) | |
96 handler.write(url,content); | |
97 } | |
98 | |
99 public static String toData(String message) { | |
100 if( message.endsWith("\n") ) | |
101 message = message.substring( 0, message.length() - 1 ); | |
102 return "data: " + message.replace( "\n", "\ndata: " ) + "\n"; | |
103 } | |
104 | |
105 public static void writeMessage( String url, String message ) { | |
106 write( url, toData(message) ); | |
107 } | |
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 | 130 private ServerSentEvents() {} // never |
131 } |