Skip to content

Commit 1abbe75

Browse files
committed
Merge pull request AsyncHttpClient#748 from elakito/work-1.8.x
[1.8.x] support ping/pong listener handling
2 parents 7f4a66d + 50a0c8c commit 1abbe75

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@
9595
import org.jboss.netty.handler.codec.http.HttpVersion;
9696
import org.jboss.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
9797
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
98+
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
99+
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
98100
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
99101
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;
100102
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder;
@@ -2274,6 +2276,8 @@ private final class WebSocketProtocol implements Protocol {
22742276
private static final byte OPCODE_CONT = 0x0;
22752277
private static final byte OPCODE_TEXT = 0x1;
22762278
private static final byte OPCODE_BINARY = 0x2;
2279+
private static final byte OPCODE_PING = 0x9;
2280+
private static final byte OPCODE_PONG = 0xa;
22772281
private static final byte OPCODE_UNKNOWN = -1;
22782282

22792283
// We don't need to synchronize as replacing the "ws-decoder" will process using the same thread.
@@ -2376,6 +2380,10 @@ public void handle(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
23762380
pendingOpcode = OPCODE_TEXT;
23772381
} else if (frame instanceof BinaryWebSocketFrame) {
23782382
pendingOpcode = OPCODE_BINARY;
2383+
} else if (frame instanceof PingWebSocketFrame) {
2384+
pendingOpcode = OPCODE_PING;
2385+
} else if (frame instanceof PongWebSocketFrame) {
2386+
pendingOpcode = OPCODE_PONG;
23792387
}
23802388

23812389
HttpChunk webSocketChunk = new HttpChunk() {
@@ -2409,6 +2417,10 @@ public void setContent(ChannelBuffer content) {
24092417
webSocket.onBinaryFragment(rp.getBodyPartBytes(), frame.isFinalFragment());
24102418
} else if (pendingOpcode == OPCODE_TEXT) {
24112419
webSocket.onTextFragment(frame.getBinaryData().toString(UTF8), frame.isFinalFragment());
2420+
} else if (pendingOpcode == OPCODE_PING) {
2421+
webSocket.onPing(rp.getBodyPartBytes());
2422+
} else if (pendingOpcode == OPCODE_PONG) {
2423+
webSocket.onPong(rp.getBodyPartBytes());
24122424
}
24132425

24142426
if (frame instanceof CloseWebSocketFrame) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import com.ning.http.client.websocket.WebSocketByteListener;
1717
import com.ning.http.client.websocket.WebSocketCloseCodeReasonListener;
1818
import com.ning.http.client.websocket.WebSocketListener;
19+
import com.ning.http.client.websocket.WebSocketPingListener;
20+
import com.ning.http.client.websocket.WebSocketPongListener;
1921
import com.ning.http.client.websocket.WebSocketTextListener;
2022
import org.jboss.netty.channel.Channel;
2123
import org.jboss.netty.channel.ChannelFutureListener;
@@ -213,6 +215,20 @@ protected void onTextFragment(String message, boolean last) {
213215
}
214216
}
215217

218+
public void onPing(byte[] payload) {
219+
for (WebSocketListener listener : listeners) {
220+
if (listener instanceof WebSocketPingListener)
221+
WebSocketPingListener.class.cast(listener).onPing(payload);
222+
}
223+
}
224+
225+
public void onPong(byte[] payload) {
226+
for (WebSocketListener listener : listeners) {
227+
if (listener instanceof WebSocketPongListener)
228+
WebSocketPongListener.class.cast(listener).onPong(payload);
229+
}
230+
}
231+
216232
protected void onError(Throwable t) {
217233
for (WebSocketListener l : listeners) {
218234
try {

0 commit comments

Comments
 (0)