|
4 | 4 | import io.jooby.Jooby; |
5 | 5 |
|
6 | 6 | import java.nio.file.Paths; |
| 7 | +import java.util.concurrent.Executors; |
| 8 | +import java.util.concurrent.ScheduledExecutorService; |
| 9 | +import java.util.concurrent.TimeUnit; |
7 | 10 | import java.util.concurrent.atomic.AtomicInteger; |
8 | 11 |
|
9 | 12 | public class WebSocketApp extends Jooby { |
10 | 13 | { |
11 | | - assets("/", Paths.get(System.getProperty("user.dir"), "examples", "www", "websocket")); |
| 14 | + assets("/?*", Paths.get(System.getProperty("user.dir"), "examples", "www", "websocket")); |
12 | 15 |
|
| 16 | + ScheduledExecutorService executor = Executors |
| 17 | + .newSingleThreadScheduledExecutor(); |
13 | 18 | ws("/ws", ctx -> { |
14 | 19 | AtomicInteger counter = new AtomicInteger(); |
15 | | -// ws.onConnect(ctx -> { |
16 | | -// System.out.println("connect: " + counter.incrementAndGet()); |
17 | | -// }); |
18 | | -// ws.onMessage((ctx, msg) -> { |
19 | | -// System.out.println("msg: " + counter.incrementAndGet() + " => " + msg); |
20 | | -// System.out.println(Thread.currentThread()); |
21 | | -// }); |
| 20 | + ctx.onConnect(ws -> { |
| 21 | + executor.scheduleWithFixedDelay(() -> { |
| 22 | + ws.send("" + counter.incrementAndGet()); |
| 23 | + }, 0, 3, TimeUnit.SECONDS); |
| 24 | + }); |
| 25 | + ctx.onMessage((ws, msg) -> { |
| 26 | + System.out.println("msg: " + counter.incrementAndGet() + " => " + msg.value()); |
| 27 | + System.out.println(Thread.currentThread()); |
| 28 | + // ws.send("Got: " + msg.value()); |
| 29 | + }); |
| 30 | + ctx.onClose((ws, closeStatus) -> { |
| 31 | + System.out.println("Closed " + closeStatus); |
| 32 | + }); |
| 33 | + |
22 | 34 | }); |
23 | 35 | } |
24 | 36 |
|
25 | 37 | public static void main(String[] args) { |
26 | 38 | runApp(args, ExecutionMode.DEFAULT, WebSocketApp::new); |
27 | | -// runApp(args, ExecutionMode.EVENT_LOOP, WebSocketApp::new); |
| 39 | + // runApp(args, ExecutionMode.EVENT_LOOP, WebSocketApp::new); |
28 | 40 | } |
29 | 41 | } |
0 commit comments