diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/frames/ControlFrame.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/frames/ControlFrame.java index ccd731ee5fa..73e51a1b06c 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/frames/ControlFrame.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/frames/ControlFrame.java @@ -21,6 +21,7 @@ package org.eclipse.jetty.websocket.common.frames; import java.nio.ByteBuffer; import java.util.Arrays; +import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.websocket.api.ProtocolException; import org.eclipse.jetty.websocket.common.WebSocketFrame; @@ -128,4 +129,14 @@ public abstract class ControlFrame extends WebSocketFrame } return super.setPayload(buf); } + + @Override + public ByteBuffer getPayload() + { + if (super.getPayload() == null) + { + return BufferUtil.EMPTY_BUFFER; + } + return super.getPayload(); + } } diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java index 9df0fbb24c0..199fad5fac0 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/events/EventDriverTest.java @@ -28,6 +28,7 @@ import org.eclipse.jetty.websocket.api.extensions.Frame; import org.eclipse.jetty.websocket.common.CloseInfo; import org.eclipse.jetty.websocket.common.frames.BinaryFrame; import org.eclipse.jetty.websocket.common.frames.PingFrame; +import org.eclipse.jetty.websocket.common.frames.PongFrame; import org.eclipse.jetty.websocket.common.frames.TextFrame; import org.eclipse.jetty.websocket.common.io.CloseableLocalWebSocketSession; import org.eclipse.jetty.websocket.common.io.LocalWebSocketSession; @@ -45,6 +46,7 @@ import examples.AnnotatedBinaryStreamSocket; import examples.AnnotatedFramesSocket; import examples.AnnotatedTextSocket; import examples.ListenerBasicSocket; +import examples.ListenerPingPongSocket; public class EventDriverTest { @@ -185,6 +187,50 @@ public class EventDriverTest } } + @Test + public void testListenerPingPong() throws Exception + { + ListenerPingPongSocket socket = new ListenerPingPongSocket(); + EventDriver driver = wrap(socket); + + try (LocalWebSocketSession conn = new CloseableLocalWebSocketSession(container,testname,driver)) + { + conn.start(); + conn.open(); + driver.incomingFrame(new PingFrame().setPayload("PING")); + driver.incomingFrame(new PongFrame().setPayload("PONG")); + driver.incomingFrame(new CloseInfo(StatusCode.NORMAL).asFrame()); + + socket.capture.assertEventCount(4); + socket.capture.pop().assertEventStartsWith("onWebSocketConnect"); + socket.capture.pop().assertEventStartsWith("onWebSocketPing("); + socket.capture.pop().assertEventStartsWith("onWebSocketPong("); + socket.capture.pop().assertEventStartsWith("onWebSocketClose(1000,"); + } + } + + @Test + public void testListenerEmptyPingPong() throws Exception + { + ListenerPingPongSocket socket = new ListenerPingPongSocket(); + EventDriver driver = wrap(socket); + + try (LocalWebSocketSession conn = new CloseableLocalWebSocketSession(container,testname,driver)) + { + conn.start(); + conn.open(); + driver.incomingFrame(new PingFrame()); + driver.incomingFrame(new PongFrame()); + driver.incomingFrame(new CloseInfo(StatusCode.NORMAL).asFrame()); + + socket.capture.assertEventCount(4); + socket.capture.pop().assertEventStartsWith("onWebSocketConnect"); + socket.capture.pop().assertEventStartsWith("onWebSocketPing("); + socket.capture.pop().assertEventStartsWith("onWebSocketPong("); + socket.capture.pop().assertEventStartsWith("onWebSocketClose(1000,"); + } + } + private EventDriver wrap(Object websocket) { WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();