com.gitee.rabbitnoteeth.bedrock.http.server.handler.WebSocketHandler Maven / Gradle / Ivy
The newest version!
package com.gitee.rabbitnoteeth.bedrock.http.server.handler;
import io.vertx.core.Handler;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.ServerWebSocket;
import java.util.Map;
public class WebSocketHandler implements Handler {
private final Map endPointMap;
public WebSocketHandler(Map endPoints) {
this.endPointMap = endPoints;
}
@Override
public void handle(ServerWebSocket socket) {
String path = socket.path();
IWebSocketEndPoint endPoint = endPointMap.get(path);
if (endPoint == null) {
socket.reject(404);
} else {
socket.writePing(Buffer.buffer("ping"));
socket.pongHandler(event -> endPoint.onOpen(socket));
socket.textMessageHandler(message -> endPoint.onMessage(socket, message));
socket.exceptionHandler(e -> endPoint.onException(socket, e));
socket.closeHandler(v -> endPoint.onClose(socket));
}
}
}