Search in sources :

Example 1 with OnWebSocketFrame

use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame in project jetty.project by eclipse.

the class EchoFragmentSocket method onFrame.

@OnWebSocketFrame
public void onFrame(Session session, Frame frame) {
    if (!frame.getType().isData()) {
        // Don't process non-data frames
        return;
    }
    ByteBuffer data = frame.getPayload();
    int half = data.remaining() / 2;
    ByteBuffer buf1 = data.slice();
    ByteBuffer buf2 = data.slice();
    buf1.limit(half);
    buf2.position(half);
    RemoteEndpoint remote = session.getRemote();
    try {
        switch(frame.getType()) {
            case BINARY:
                remote.sendBytes(buf1, null);
                remote.sendBytes(buf2, null);
                break;
            case TEXT:
                // NOTE: This impl is not smart enough to split on a UTF8 boundary
                remote.sendString(BufferUtil.toUTF8String(buf1), null);
                remote.sendString(BufferUtil.toUTF8String(buf2), null);
                break;
            default:
                throw new IOException("Unexpected frame type: " + frame.getType());
        }
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) OnWebSocketFrame(org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)

Example 2 with OnWebSocketFrame

use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame in project spring-framework by spring-projects.

the class JettyWebSocketHandlerAdapter method onWebSocketFrame.

@OnWebSocketFrame
public void onWebSocketFrame(Frame frame) {
    if (this.delegateSession != null) {
        if (OpCode.PONG == frame.getOpCode()) {
            ByteBuffer buffer = (frame.getPayload() != null ? frame.getPayload() : EMPTY_PAYLOAD);
            WebSocketMessage webSocketMessage = toMessage(Type.PONG, buffer);
            delegateSession.handleMessage(webSocketMessage.getType(), webSocketMessage);
        }
    }
}
Also used : OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) ByteBuffer(java.nio.ByteBuffer) OnWebSocketFrame(org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)

Example 3 with OnWebSocketFrame

use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame in project spring-framework by spring-projects.

the class JettyWebSocketHandlerAdapter method onWebSocketFrame.

@OnWebSocketFrame
public void onWebSocketFrame(Frame frame) {
    if (OpCode.PONG == frame.getOpCode()) {
        ByteBuffer payload = frame.getPayload() != null ? frame.getPayload() : EMPTY_PAYLOAD;
        PongMessage message = new PongMessage(payload);
        try {
            this.webSocketHandler.handleMessage(this.wsSession, message);
        } catch (Throwable ex) {
            ExceptionWebSocketHandlerDecorator.tryCloseWithError(this.wsSession, ex, logger);
        }
    }
}
Also used : PongMessage(org.springframework.web.socket.PongMessage) ByteBuffer(java.nio.ByteBuffer) OnWebSocketFrame(org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)

Aggregations

ByteBuffer (java.nio.ByteBuffer)3 OnWebSocketFrame (org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)3 IOException (java.io.IOException)1 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)1 OnWebSocketMessage (org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)1 WebSocketMessage (org.springframework.web.reactive.socket.WebSocketMessage)1 PongMessage (org.springframework.web.socket.PongMessage)1