Skip to content

[1.8.x] support ping/pong listener handling #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PingWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameDecoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocket08FrameEncoder;
Expand Down Expand Up @@ -2274,6 +2276,8 @@ private final class WebSocketProtocol implements Protocol {
private static final byte OPCODE_CONT = 0x0;
private static final byte OPCODE_TEXT = 0x1;
private static final byte OPCODE_BINARY = 0x2;
private static final byte OPCODE_PING = 0x9;
private static final byte OPCODE_PONG = 0xa;
private static final byte OPCODE_UNKNOWN = -1;

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

HttpChunk webSocketChunk = new HttpChunk() {
Expand Down Expand Up @@ -2409,6 +2417,10 @@ public void setContent(ChannelBuffer content) {
webSocket.onBinaryFragment(rp.getBodyPartBytes(), frame.isFinalFragment());
} else if (pendingOpcode == OPCODE_TEXT) {
webSocket.onTextFragment(frame.getBinaryData().toString(UTF8), frame.isFinalFragment());
} else if (pendingOpcode == OPCODE_PING) {
webSocket.onPing(rp.getBodyPartBytes());
} else if (pendingOpcode == OPCODE_PONG) {
webSocket.onPong(rp.getBodyPartBytes());
}

if (frame instanceof CloseWebSocketFrame) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.ning.http.client.websocket.WebSocketByteListener;
import com.ning.http.client.websocket.WebSocketCloseCodeReasonListener;
import com.ning.http.client.websocket.WebSocketListener;
import com.ning.http.client.websocket.WebSocketPingListener;
import com.ning.http.client.websocket.WebSocketPongListener;
import com.ning.http.client.websocket.WebSocketTextListener;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFutureListener;
Expand Down Expand Up @@ -213,6 +215,20 @@ protected void onTextFragment(String message, boolean last) {
}
}

public void onPing(byte[] payload) {
for (WebSocketListener listener : listeners) {
if (listener instanceof WebSocketPingListener)
WebSocketPingListener.class.cast(listener).onPing(payload);
}
}

public void onPong(byte[] payload) {
for (WebSocketListener listener : listeners) {
if (listener instanceof WebSocketPongListener)
WebSocketPongListener.class.cast(listener).onPong(payload);
}
}

protected void onError(Throwable t) {
for (WebSocketListener l : listeners) {
try {
Expand Down