Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit c167c51

Browse files
committed
MVC should support WebSockets fix jooby-project#548
* rename web socket callbacks * implement class/mvc listener using new callbacks ```java @path("/ws") @consumes("json") @produces("json") class MyHandler implements WebSocket.OnMessage<MyObject> { public void onMessage(MyObject object) { ws.send(new MyResponseObject()); } } ```
1 parent 108aea5 commit c167c51

24 files changed

Lines changed: 1927 additions & 224 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.jooby.issues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.LinkedList;
7+
import java.util.concurrent.CountDownLatch;
8+
import java.util.concurrent.TimeUnit;
9+
10+
import javax.inject.Inject;
11+
12+
import org.jooby.test.ServerFeature;
13+
import org.junit.After;
14+
import org.junit.Before;
15+
import org.junit.Test;
16+
17+
import com.ning.http.client.AsyncHttpClient;
18+
import com.ning.http.client.AsyncHttpClientConfig;
19+
import com.ning.http.client.ws.WebSocket;
20+
import com.ning.http.client.ws.WebSocketTextListener;
21+
import com.ning.http.client.ws.WebSocketUpgradeHandler;
22+
23+
public class Issue548 extends ServerFeature {
24+
25+
public static class MySocket implements org.jooby.WebSocket.OnMessage<String> {
26+
27+
private org.jooby.WebSocket ws;
28+
29+
@Inject
30+
public MySocket(final org.jooby.WebSocket ws) {
31+
this.ws = ws;
32+
}
33+
34+
@Override
35+
public void onMessage(final String message) throws Exception {
36+
ws.send("=" + message);
37+
}
38+
39+
}
40+
41+
{
42+
ws("/548/mvcws", MySocket.class);
43+
}
44+
45+
private AsyncHttpClient client;
46+
47+
@Before
48+
public void before() {
49+
client = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().build());
50+
}
51+
52+
@After
53+
public void after() {
54+
client.close();
55+
}
56+
57+
@Test
58+
public void sendText() throws Exception {
59+
LinkedList<String> messages = new LinkedList<>();
60+
61+
CountDownLatch latch = new CountDownLatch(1);
62+
63+
client.prepareGet(ws("548/mvcws").toString())
64+
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
65+
new WebSocketTextListener() {
66+
67+
@Override
68+
public void onMessage(final String message) {
69+
messages.add(message);
70+
latch.countDown();
71+
}
72+
73+
@Override
74+
public void onOpen(final WebSocket websocket) {
75+
websocket.sendMessage("mvc");
76+
}
77+
78+
@Override
79+
public void onClose(final WebSocket websocket) {
80+
}
81+
82+
@Override
83+
public void onError(final Throwable t) {
84+
}
85+
}).build())
86+
.get();
87+
if (latch.await(1L, TimeUnit.SECONDS)) {
88+
assertEquals(Arrays.asList("=mvc"), messages);
89+
}
90+
}
91+
92+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.jooby.issues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.LinkedList;
6+
import java.util.concurrent.CountDownLatch;
7+
import java.util.concurrent.TimeUnit;
8+
9+
import javax.inject.Inject;
10+
11+
import org.jooby.json.Jackson;
12+
import org.jooby.mvc.Consumes;
13+
import org.jooby.mvc.Path;
14+
import org.jooby.mvc.Produces;
15+
import org.jooby.test.ServerFeature;
16+
import org.junit.After;
17+
import org.junit.Before;
18+
import org.junit.Test;
19+
20+
import com.ning.http.client.AsyncHttpClient;
21+
import com.ning.http.client.AsyncHttpClientConfig;
22+
import com.ning.http.client.ws.WebSocket;
23+
import com.ning.http.client.ws.WebSocketTextListener;
24+
import com.ning.http.client.ws.WebSocketUpgradeHandler;
25+
26+
public class Issue548b extends ServerFeature {
27+
28+
public static class User {
29+
public String name;
30+
31+
@Override
32+
public String toString() {
33+
return name;
34+
}
35+
}
36+
37+
@Path("/548d/more-annotations")
38+
@Produces("json")
39+
@Consumes("json")
40+
public static class MySocket implements org.jooby.WebSocket.OnMessage<User> {
41+
42+
private org.jooby.WebSocket ws;
43+
44+
@Inject
45+
public MySocket(final org.jooby.WebSocket ws) {
46+
this.ws = ws;
47+
}
48+
49+
@Override
50+
public void onMessage(final User message) throws Exception {
51+
User rsp = new User();
52+
rsp.name = message.name + " marmol";
53+
ws.send(rsp);
54+
}
55+
56+
}
57+
58+
{
59+
use(new Jackson());
60+
61+
ws(MySocket.class);
62+
}
63+
64+
private AsyncHttpClient client;
65+
66+
@Before
67+
public void before() {
68+
client = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().build());
69+
}
70+
71+
@After
72+
public void after() {
73+
client.close();
74+
}
75+
76+
@Test
77+
public void shouldParseAndRenderJsonViaAnnotationsOnTopLevelClass() throws Exception {
78+
LinkedList<String> messages = new LinkedList<>();
79+
80+
CountDownLatch latch = new CountDownLatch(1);
81+
82+
client.prepareGet(ws("548d/more-annotations").toString())
83+
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
84+
new WebSocketTextListener() {
85+
86+
@Override
87+
public void onMessage(final String message) {
88+
messages.add(message);
89+
latch.countDown();
90+
}
91+
92+
@Override
93+
public void onOpen(final WebSocket websocket) {
94+
websocket.sendMessage("{\"name\":\"pablo\"}");
95+
}
96+
97+
@Override
98+
public void onClose(final WebSocket websocket) {
99+
}
100+
101+
@Override
102+
public void onError(final Throwable t) {
103+
}
104+
}).build())
105+
.get();
106+
if (latch.await(1L, TimeUnit.SECONDS)) {
107+
assertEquals("{\"name\":\"pablo marmol\"}", messages.get(0));
108+
}
109+
}
110+
111+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.jooby.issues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.LinkedList;
7+
import java.util.concurrent.CountDownLatch;
8+
import java.util.concurrent.TimeUnit;
9+
10+
import javax.inject.Inject;
11+
12+
import org.jooby.mvc.Path;
13+
import org.jooby.test.ServerFeature;
14+
import org.junit.After;
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
18+
import com.ning.http.client.AsyncHttpClient;
19+
import com.ning.http.client.AsyncHttpClientConfig;
20+
import com.ning.http.client.ws.WebSocket;
21+
import com.ning.http.client.ws.WebSocketTextListener;
22+
import com.ning.http.client.ws.WebSocketUpgradeHandler;
23+
24+
public class Issue548c extends ServerFeature {
25+
26+
@Path("/548c/annotated-path")
27+
public static class MySocket implements org.jooby.WebSocket.OnMessage<String> {
28+
29+
private org.jooby.WebSocket ws;
30+
31+
@Inject
32+
public MySocket(final org.jooby.WebSocket ws) {
33+
this.ws = ws;
34+
}
35+
36+
@Override
37+
public void onMessage(final String message) throws Exception {
38+
ws.send("=" + message);
39+
}
40+
41+
}
42+
43+
{
44+
ws(MySocket.class);
45+
}
46+
47+
private AsyncHttpClient client;
48+
49+
@Before
50+
public void before() {
51+
client = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().build());
52+
}
53+
54+
@After
55+
public void after() {
56+
client.close();
57+
}
58+
59+
@Test
60+
public void sendText() throws Exception {
61+
LinkedList<String> messages = new LinkedList<>();
62+
63+
CountDownLatch latch = new CountDownLatch(1);
64+
65+
client.prepareGet(ws("548c/annotated-path").toString())
66+
.execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(
67+
new WebSocketTextListener() {
68+
69+
@Override
70+
public void onMessage(final String message) {
71+
messages.add(message);
72+
latch.countDown();
73+
}
74+
75+
@Override
76+
public void onOpen(final WebSocket websocket) {
77+
websocket.sendMessage("mvc");
78+
}
79+
80+
@Override
81+
public void onClose(final WebSocket websocket) {
82+
}
83+
84+
@Override
85+
public void onError(final Throwable t) {
86+
}
87+
}).build())
88+
.get();
89+
if (latch.await(1L, TimeUnit.SECONDS)) {
90+
assertEquals(Arrays.asList("=mvc"), messages);
91+
}
92+
}
93+
94+
}

0 commit comments

Comments
 (0)