|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "WebSocket" |
| 4 | +category: doc |
| 5 | +date: 2017-07-20 20:03:09 |
| 6 | +order: 200 |
| 7 | +--- |
| 8 | + |
| 9 | +#### How to use |
| 10 | + |
| 11 | +Pippo allows you to use [WebSocket](https://en.wikipedia.org/wiki/WebSocket) in your application in an easy and uniform mode. |
| 12 | +You use the same interface for all major embedded web server supported by Pippo builtin (Jetty, Tomcat and Undertow). |
| 13 | + |
| 14 | +The `Application` class contains a method with the signature: |
| 15 | + |
| 16 | +``` java |
| 17 | +public void addWebSocket(String path, WebSocketHandler webSocketHandler); |
| 18 | +``` |
| 19 | + |
| 20 | +`WebSocketHandler` is an interface (functional) that contains one method |
| 21 | + |
| 22 | +``` java |
| 23 | +void onMessage(WebSocketContext webSocketContext, String message); |
| 24 | +``` |
| 25 | + |
| 26 | +and some default methods. |
| 27 | + |
| 28 | +To add an echo application based on websocket you should write something similar with: |
| 29 | + |
| 30 | +``` java |
| 31 | +// add web socket |
| 32 | +addWebSocket("/ws/echo", (webSocketContext, message) -> { |
| 33 | + try { |
| 34 | + webSocketContext.sendMessage(message); |
| 35 | + } catch (IOException e) { |
| 36 | + e.printStackTrace(); |
| 37 | + } |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +If you need more control you can override other methods available in `WebSocketHandler`: |
| 42 | + |
| 43 | +``` java |
| 44 | +addWebSocket("/ws/echo", new WebSocketHandler() { |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onMessage(WebSocketContext webSocketContext, String message) { |
| 48 | + System.out.println("TestWebSocket.onMessage"); |
| 49 | + System.out.println("message = " + message); |
| 50 | + try { |
| 51 | + webSocketContext.sendMessage(message); |
| 52 | + } catch (IOException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onMessage(WebSocketContext webSocketContext, byte[] message) { |
| 59 | + System.out.println("TestWebSocket.onMessage"); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void onOpen(WebSocketContext webSocketContext) { |
| 64 | + System.out.println("TestWebSocket.onOpen"); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onClose(WebSocketContext webSocketContext, int closeCode, String message) { |
| 69 | + System.out.println("TestWebSocket.onClose"); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void onTimeout(WebSocketContext webSocketContext) { |
| 74 | + System.out.println("TestWebSocket.onTimeout"); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void onError(WebSocketContext webSocketContext, Throwable t) { |
| 79 | + System.out.println("TestWebSocket.onError"); |
| 80 | + } |
| 81 | + |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +In above example, I used a standard `Route` to serve the `index.html` file that contains the script block with the websocket client side: |
| 86 | + |
| 87 | +``` java |
| 88 | +GET("/", routeContext -> { |
| 89 | + try { |
| 90 | + routeContext.send(IoUtils.toString(WebSocketApplication.class.getResourceAsStream("/index.html"))); |
| 91 | + } catch (IOException e) { |
| 92 | + e.printStackTrace(); |
| 93 | + } |
| 94 | +}); |
| 95 | + |
| 96 | +// OR |
| 97 | + |
| 98 | +DirectoryHandler directoryHandler = new DirectoryHandler("/", new File("src/main/resources")); |
| 99 | +GET(directoryHandler.getUriPattern(), directoryHandler); |
| 100 | +``` |
| 101 | + |
| 102 | +**Note** A functional demo is available to [pippo-demo-websocket]({{ site.demourl }}/pippo-demo-websocket). |
0 commit comments