Skip to content

Commit 7a00b5f

Browse files
committed
Resolve #61
1 parent 41ab97f commit 7a00b5f

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

_posts/2015-03-17-demo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: page
33
title: "Demo"
44
category: doc
55
date: 2015-03-17 17:42:47
6-
order: 200
6+
order: 210
77
---
88

99
The demo applications are in [pippo-demo](https://github.com/decebals/pippo-demo) repository:

_posts/2016-08-08-deployment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: page
33
title: "Deployment"
44
category: doc
55
date: 2016-08-08 09:46:53
6-
order: 201
6+
order: 220
77
---
88

99
#### Packaging

_posts/2017-07-18-hot-reloading.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ order: 199
77
---
88

99
#### How to use
10+
1011
This feature allows you to write code, save the code on disk and reload the web page from browser without restart the application (from IDE).
1112

1213
```java
@@ -41,6 +42,7 @@ You can supply the application class name using one of the below methods:
4142
- use `-Dpippo.applicationClassName` system property
4243

4344
#### How works
45+
4446
To implement hot reloading, Pippo introduces two new concepts (classes in package `ro.pippo.core.reload`):
4547

4648
- `ReloadWatcher`

_posts/2017-07-20-websocket.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)