Search in sources :

Example 1 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project alien4cloud by alien4cloud.

the class StompClientHandler method beginStomp.

public void beginStomp(Channel channel) throws Exception {
    StompFrame connFrame = new DefaultStompFrame(StompCommand.CONNECT);
    connFrame.headers().set(StompHeaders.ACCEPT_VERSION, "1.2");
    channel.writeAndFlush(connFrame);
    if (log.isDebugEnabled()) {
        log.debug("Begin web socket connection");
    }
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 2 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.

the class StompClientHandler method channelActive.

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    state = ClientState.AUTHENTICATING;
    StompFrame connFrame = new DefaultStompFrame(StompCommand.CONNECT);
    connFrame.headers().set(StompHeaders.ACCEPT_VERSION, "1.2");
    connFrame.headers().set(StompHeaders.HOST, StompClient.HOST);
    connFrame.headers().set(StompHeaders.LOGIN, StompClient.LOGIN);
    connFrame.headers().set(StompHeaders.PASSCODE, StompClient.PASSCODE);
    ctx.writeAndFlush(connFrame);
}
Also used : DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 3 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.

the class StompChatHandler method onSubscribe.

private void onSubscribe(ChannelHandlerContext ctx, StompFrame inboundFrame) {
    String destination = inboundFrame.headers().getAsString(DESTINATION);
    String subscriptionId = inboundFrame.headers().getAsString(ID);
    if (destination == null || subscriptionId == null) {
        sendErrorFrame("missed header", "Required 'destination' or 'id' header missed", ctx);
        return;
    }
    Set<StompSubscription> subscriptions = chatDestinations.get(destination);
    if (subscriptions == null) {
        subscriptions = new HashSet<StompSubscription>();
        Set<StompSubscription> previousSubscriptions = chatDestinations.putIfAbsent(destination, subscriptions);
        if (previousSubscriptions != null) {
            subscriptions = previousSubscriptions;
        }
    }
    final StompSubscription subscription = new StompSubscription(subscriptionId, destination, ctx.channel());
    if (subscriptions.contains(subscription)) {
        sendErrorFrame("duplicate subscription", "Received duplicate subscription id=" + subscriptionId, ctx);
        return;
    }
    subscriptions.add(subscription);
    ctx.channel().closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) {
            chatDestinations.get(subscription.destination()).remove(subscription);
        }
    });
    String receiptId = inboundFrame.headers().getAsString(RECEIPT);
    if (receiptId != null) {
        StompFrame receiptFrame = new DefaultStompFrame(StompCommand.RECEIPT);
        receiptFrame.headers().set(RECEIPT_ID, receiptId);
        ctx.writeAndFlush(receiptFrame);
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) ChannelFutureListener(io.netty.channel.ChannelFutureListener) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 4 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.

the class StompChatHandler method onConnect.

private static void onConnect(ChannelHandlerContext ctx, StompFrame inboundFrame) {
    String acceptVersions = inboundFrame.headers().getAsString(ACCEPT_VERSION);
    StompVersion handshakeAcceptVersion = ctx.channel().attr(StompVersion.CHANNEL_ATTRIBUTE_KEY).get();
    if (acceptVersions == null || !acceptVersions.contains(handshakeAcceptVersion.version())) {
        sendErrorFrame("invalid version", "Received invalid version, expected " + handshakeAcceptVersion.version(), ctx);
        return;
    }
    StompFrame connectedFrame = new DefaultStompFrame(StompCommand.CONNECTED);
    connectedFrame.headers().set(VERSION, handshakeAcceptVersion.version()).set(SERVER, "Netty-Server").set(HEART_BEAT, "0,0");
    ctx.writeAndFlush(connectedFrame);
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Example 5 with DefaultStompFrame

use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.

the class StompChatHandler method onDisconnect.

private static void onDisconnect(ChannelHandlerContext ctx, StompFrame inboundFrame) {
    String receiptId = inboundFrame.headers().getAsString(RECEIPT);
    if (receiptId == null) {
        ctx.close();
        return;
    }
    StompFrame receiptFrame = new DefaultStompFrame(StompCommand.RECEIPT);
    receiptFrame.headers().set(RECEIPT_ID, receiptId);
    ctx.writeAndFlush(receiptFrame).addListener(ChannelFutureListener.CLOSE);
}
Also used : StompFrame(io.netty.handler.codec.stomp.StompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame) DefaultStompFrame(io.netty.handler.codec.stomp.DefaultStompFrame)

Aggregations

DefaultStompFrame (io.netty.handler.codec.stomp.DefaultStompFrame)10 StompFrame (io.netty.handler.codec.stomp.StompFrame)9 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ByteBuf (io.netty.buffer.ByteBuf)1 StompHeadersSubframe (io.netty.handler.codec.stomp.StompHeadersSubframe)1 StompSubframeEncoder (io.netty.handler.codec.stomp.StompSubframeEncoder)1 EmbeddedChannelWriteReleaseHandlerContext (io.netty.microbench.channel.EmbeddedChannelWriteReleaseHandlerContext)1 URISyntaxException (java.net.URISyntaxException)1 Setup (org.openjdk.jmh.annotations.Setup)1