forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[websocket] support pong in websocket (apache#10035)
Fixes apache#10014 ### Motivation Add "pong" logic in websocket server ### Modifications add WebSocketPingPongServlet to apply "pong" when on ping
- Loading branch information
Showing
6 changed files
with
243 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
pulsar-websocket/src/main/java/org/apache/pulsar/websocket/PingPongHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.websocket; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import org.eclipse.jetty.util.BufferUtil; | ||
import org.eclipse.jetty.websocket.api.WebSocketAdapter; | ||
import org.eclipse.jetty.websocket.api.WebSocketPingPongListener; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class PingPongHandler extends WebSocketAdapter implements WebSocketPingPongListener { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PingPongHandler.class); | ||
|
||
@Override | ||
public void onWebSocketPing(ByteBuffer payload) { | ||
try { | ||
if (log.isDebugEnabled()) { | ||
log.debug("PING: {}", BufferUtil.toDetailString(payload)); | ||
} | ||
getRemote().sendPong(payload); | ||
} catch (IOException e) { | ||
log.warn("Failed to send pong: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public void onWebSocketPong(ByteBuffer payload) { | ||
|
||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
pulsar-websocket/src/main/java/org/apache/pulsar/websocket/WebSocketPingPongServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.websocket; | ||
|
||
import org.eclipse.jetty.websocket.servlet.WebSocketServlet; | ||
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; | ||
|
||
public class WebSocketPingPongServlet extends WebSocketServlet { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public static final String SERVLET_PATH = "/ws/pingpong"; | ||
public static final String SERVLET_PATH_V2 = "/ws/v2/pingpong"; | ||
|
||
private final transient WebSocketService service; | ||
|
||
public WebSocketPingPongServlet(WebSocketService service) { | ||
this.service = service; | ||
} | ||
|
||
@Override | ||
public void configure(WebSocketServletFactory factory) { | ||
factory.getPolicy().setMaxTextMessageSize(service.getConfig().getWebSocketMaxTextFrameSize()); | ||
if (service.getConfig().getWebSocketSessionIdleTimeoutMillis() > 0) { | ||
factory.getPolicy().setIdleTimeout(service.getConfig().getWebSocketSessionIdleTimeoutMillis()); | ||
} | ||
factory.setCreator((request, response) -> new PingPongHandler()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
pulsar-websocket/src/test/java/org/apache/pulsar/websocket/PingPongHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.websocket; | ||
|
||
import java.net.URI; | ||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.ArrayBlockingQueue; | ||
import java.util.concurrent.Future; | ||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
import org.apache.pulsar.broker.web.WebExecutorThreadPool; | ||
import org.eclipse.jetty.client.HttpClient; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.servlet.ServletContextHandler; | ||
import org.eclipse.jetty.servlet.ServletHolder; | ||
import org.eclipse.jetty.util.BufferUtil; | ||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.api.WebSocketAdapter; | ||
import org.eclipse.jetty.websocket.api.WebSocketPingPongListener; | ||
import org.eclipse.jetty.websocket.api.annotations.WebSocket; | ||
import org.eclipse.jetty.websocket.client.WebSocketClient; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertTrue; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
public class PingPongHandlerTest { | ||
|
||
private static Server server; | ||
|
||
private static final WebExecutorThreadPool executor = new WebExecutorThreadPool(6, "pulsar-websocket-web-test"); | ||
|
||
@BeforeClass | ||
public static void setup() throws Exception { | ||
server = new Server(executor); | ||
List<ServerConnector> connectors = new ArrayList<>(); | ||
ServerConnector connector = new ServerConnector(server); | ||
connector.setPort(8080); | ||
connectors.add(connector); | ||
connectors.forEach(c -> c.setAcceptQueueSize(1024 / connectors.size())); | ||
server.setConnectors(connectors.toArray(new ServerConnector[connectors.size()])); | ||
|
||
WebSocketService service = mock(WebSocketService.class); | ||
ServiceConfiguration config = mock(ServiceConfiguration.class); | ||
|
||
when(service.getConfig()).thenReturn(config); | ||
when(config.getWebSocketMaxTextFrameSize()).thenReturn(1048576); | ||
when(config.getWebSocketSessionIdleTimeoutMillis()).thenReturn(300000); | ||
|
||
ServletHolder servletHolder = new ServletHolder("ws-events", new WebSocketPingPongServlet(service)); | ||
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); | ||
context.setContextPath(WebSocketPingPongServlet.SERVLET_PATH); | ||
context.addServlet(servletHolder, "/*"); | ||
server.setHandler(context); | ||
try { | ||
server.start(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@AfterClass(alwaysRun = true) | ||
public static void tearDown() throws Exception { | ||
if (server != null) { | ||
server.stop(); | ||
} | ||
executor.stop(); | ||
} | ||
|
||
@Test | ||
public void testPingPong() throws Exception { | ||
HttpClient httpClient = new HttpClient(); | ||
WebSocketClient webSocketClient = new WebSocketClient(httpClient); | ||
webSocketClient.start(); | ||
MyWebSocket myWebSocket = new MyWebSocket(); | ||
String webSocketUri = "ws://localhost:8080/ws/pingpong"; | ||
Future<Session> sessionFuture = webSocketClient.connect(myWebSocket, URI.create(webSocketUri)); | ||
sessionFuture.get().getRemote().sendPing(ByteBuffer.wrap("test".getBytes())); | ||
assertTrue(myWebSocket.getResponse().contains("test")); | ||
} | ||
|
||
@WebSocket | ||
public static class MyWebSocket extends WebSocketAdapter implements WebSocketPingPongListener { | ||
|
||
ArrayBlockingQueue<String> incomingMessages = new ArrayBlockingQueue<>(10); | ||
|
||
@Override | ||
public void onWebSocketClose(int i, String s) { | ||
} | ||
|
||
@Override | ||
public void onWebSocketConnect(Session session) { | ||
} | ||
|
||
@Override | ||
public void onWebSocketError(Throwable throwable) { | ||
} | ||
|
||
@Override | ||
public void onWebSocketPing(ByteBuffer payload) { | ||
} | ||
|
||
@Override | ||
public void onWebSocketPong(ByteBuffer payload) { | ||
incomingMessages.add(BufferUtil.toDetailString(payload)); | ||
} | ||
|
||
public String getResponse() throws InterruptedException { | ||
return incomingMessages.take(); | ||
} | ||
} | ||
} |