Skip to content

Commit

Permalink
Merge pull request #8356 from eclipse/jetty-10.0.x-8353-websocketPong…
Browse files Browse the repository at this point in the history
…AfterClose

Issue #8353 - websocket automatic pong response frames
  • Loading branch information
lachlan-roberts authored Aug 17, 2022
2 parents 3157256 + 030978d commit 5fb4130
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,11 @@ private void acceptMessage(Frame frame, Callback callback)

public void onPing(Frame frame, Callback callback)
{
ByteBuffer payload = BufferUtil.copy(frame.getPayload());
coreSession.sendFrame(new Frame(OpCode.PONG).setPayload(payload), Callback.NOOP, false);
callback.succeeded();
coreSession.demand(1);
coreSession.sendFrame(new Frame(OpCode.PONG).setPayload(frame.getPayload()), Callback.from(() ->
{
callback.succeeded();
coreSession.demand(1);
}), false);
}

public void onPong(Frame frame, Callback callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,31 @@ private void onPingFrame(Frame frame, Callback callback)
{
throw new WebSocketException(endpointInstance.getClass().getSimpleName() + " PING method error: " + cause.getMessage(), cause);
}

callback.succeeded();
demand();
}
else
{
// Automatically respond
ByteBuffer payload = BufferUtil.copy(frame.getPayload());
getSession().getRemote().sendPong(payload, WriteCallback.NOOP);
// Automatically respond.
getSession().getRemote().sendPong(frame.getPayload(), new WriteCallback()
{
@Override
public void writeSuccess()
{
callback.succeeded();
demand();
}

@Override
public void writeFailed(Throwable x)
{
// Ignore failures, we might be output closed and receive ping.
callback.succeeded();
demand();
}
});
}

callback.succeeded();
demand();
}

private void onPongFrame(Frame frame, Callback callback)
Expand Down

0 comments on commit 5fb4130

Please sign in to comment.