Skip to content

Commit 3c1e8ae

Browse files
committed
Add:
- streaming support - the ability to send pings and pongs Tests to come in another commit.
1 parent 32c7e80 commit 3c1e8ae

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@
447447
<dependency>
448448
<groupId>org.glassfish.grizzly</groupId>
449449
<artifactId>grizzly-websockets</artifactId>
450-
<version>2.2</version>
450+
<version>2.2.1</version>
451451
<optional>true</optional>
452452
</dependency>
453453
</dependencies>

src/main/java/com/ning/http/client/providers/grizzly/GrizzlyAsyncHttpProvider.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,12 +2557,46 @@ public WebSocket sendMessage(byte[] message) {
25572557
return this;
25582558
}
25592559

2560+
@Override
2561+
public WebSocket stream(byte[] fragment, boolean last) {
2562+
if (fragment != null && fragment.length > 0) {
2563+
gWebSocket.stream(last, fragment, 0, fragment.length);
2564+
}
2565+
return this;
2566+
}
2567+
2568+
@Override
2569+
public WebSocket stream(byte[] fragment, int offset, int len, boolean last) {
2570+
if (fragment != null && fragment.length > 0) {
2571+
gWebSocket.stream(last, fragment, offset, len);
2572+
}
2573+
return this;
2574+
}
2575+
25602576
@Override
25612577
public WebSocket sendTextMessage(String message) {
25622578
gWebSocket.send(message);
25632579
return this;
25642580
}
25652581

2582+
@Override
2583+
public WebSocket streamText(String fragment, boolean last) {
2584+
gWebSocket.stream(last, fragment);
2585+
return this;
2586+
}
2587+
2588+
@Override
2589+
public WebSocket sendPing(byte[] payload) {
2590+
gWebSocket.sendPing(payload);
2591+
return this;
2592+
}
2593+
2594+
@Override
2595+
public WebSocket sendPong(byte[] payload) {
2596+
gWebSocket.sendPong(payload);
2597+
return this;
2598+
}
2599+
25662600
@Override
25672601
public WebSocket addMessageListener(WebSocketListener l) {
25682602
gWebSocket.add(new AHCWebSocketListenerAdapter(l, this));

src/main/java/com/ning/http/client/providers/netty/NettyWebSocket.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313
package com.ning.http.client.providers.netty;
1414

1515
import com.ning.http.client.providers.netty.netty4.BinaryWebSocketFrame;
16+
import com.ning.http.client.providers.netty.netty4.PingWebSocketFrame;
17+
import com.ning.http.client.providers.netty.netty4.PongWebSocketFrame;
1618
import com.ning.http.client.providers.netty.netty4.TextWebSocketFrame;
1719
import com.ning.http.client.websocket.WebSocket;
1820
import com.ning.http.client.websocket.WebSocketByteListener;
1921
import com.ning.http.client.websocket.WebSocketListener;
2022
import com.ning.http.client.websocket.WebSocketTextListener;
21-
import org.jboss.netty.buffer.ChannelBuffers;
2223
import org.jboss.netty.channel.Channel;
2324

24-
import java.io.UnsupportedEncodingException;
2525
import java.util.concurrent.ConcurrentLinkedQueue;
2626

2727
import static org.jboss.netty.buffer.ChannelBuffers.wrappedBuffer;
@@ -41,12 +41,46 @@ public WebSocket sendMessage(byte[] message) {
4141
return this;
4242
}
4343

44+
@Override
45+
public WebSocket stream(byte[] fragment, boolean last) {
46+
if (fragment != null && fragment.length > 0) {
47+
channel.write(new BinaryWebSocketFrame(last, 0, wrappedBuffer(fragment)));
48+
}
49+
return this;
50+
}
51+
52+
@Override
53+
public WebSocket stream(byte[] fragment, int offset, int len, boolean last) {
54+
if (fragment != null && fragment.length > 0) {
55+
channel.write(new BinaryWebSocketFrame(last, 0, wrappedBuffer(fragment, offset, len)));
56+
}
57+
return this;
58+
}
59+
4460
@Override
4561
public WebSocket sendTextMessage(String message) {
4662
channel.write(new TextWebSocketFrame(message));
4763
return this;
4864
}
4965

66+
@Override
67+
public WebSocket streamText(String fragment, boolean last) {
68+
channel.write(new TextWebSocketFrame(last, 0, fragment));
69+
return this;
70+
}
71+
72+
@Override
73+
public WebSocket sendPing(byte[] payload) {
74+
channel.write(new PingWebSocketFrame(wrappedBuffer(payload)));
75+
return this;
76+
}
77+
78+
@Override
79+
public WebSocket sendPong(byte[] payload) {
80+
channel.write(new PongWebSocketFrame(wrappedBuffer(payload)));
81+
return this;
82+
}
83+
5084
@Override
5185
public WebSocket addMessageListener(WebSocketListener l) {
5286
listeners.add(l);

src/main/java/com/ning/http/client/websocket/WebSocket.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,68 @@
1818
public interface WebSocket {
1919

2020
/**
21-
* Sen a byte message.
21+
* Send a byte message.
2222
* @param message a byte message
2323
* @return this
2424
*/
2525
WebSocket sendMessage(byte[] message);
2626

27+
/**
28+
* Allows streaming of multiple binary fragments.
29+
*
30+
* @param fragment binary fragment.
31+
* @param last flag indicating whether or not this is the last fragment.
32+
*
33+
* @return this.
34+
*/
35+
WebSocket stream(byte[] fragment, boolean last);
36+
37+
/**
38+
* Allows streaming of multiple binary fragments.
39+
*
40+
* @param fragment binary fragment.
41+
* @param offset starting offset.
42+
* @param len length.
43+
* @param last flag indicating whether or not this is the last fragment.
44+
* @return
45+
*/
46+
WebSocket stream(byte[] fragment, int offset, int len, boolean last);
47+
2748
/**
2849
* Send a text message
2950
* @param message a text message
3051
* @return this.
3152
*/
3253
WebSocket sendTextMessage(String message);
3354

55+
/**
56+
* Allows streaming of multiple text fragments.
57+
*
58+
* @param fragment text fragment.
59+
* @param last flag indicating whether or not this is the last fragment.
60+
* @return this.
61+
*/
62+
WebSocket streamText(String fragment, boolean last);
63+
64+
/**
65+
* Send a <code>ping</ping> with an optional payload
66+
* (limited to 125 bytes or less).
67+
*
68+
* @param payload the ping payload.
69+
*
70+
* @return this.
71+
*/
72+
WebSocket sendPing(byte[] payload);
73+
74+
/**
75+
* Send a <code>ping</ping> with an optional payload
76+
* (limited to 125 bytes or less).
77+
*
78+
* @param payload the pong payload.
79+
* @return this.
80+
*/
81+
WebSocket sendPong(byte[] payload);
82+
3483
/**
3584
* Add a {@link WebSocketListener}
3685
* @param l a {@link WebSocketListener}

0 commit comments

Comments
 (0)