use of io.netty.handler.codec.stomp.DefaultStompFrame in project alien4cloud by alien4cloud.
the class StompConnection method listen.
/**
* Start listening on web socket for specific data type. When data arrived the callback onData method will be called, when error happens the onError method
* will be notified
*
* @param topic the topic to listen to
* @param callback the callback to notify data or error
* @param <T> type of the data
*/
public <T> void listen(final String topic, IStompCallback<T> callback) {
this.stompClientHandler.listen(topic, callback);
this.stompClientHandler.connectFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(final ChannelFuture future) throws Exception {
StompFrame subscribeFrame = new DefaultStompFrame(StompCommand.SUBSCRIBE);
subscribeFrame.headers().set(StompHeaders.DESTINATION, topic);
subscribeFrame.headers().set(StompHeaders.ID, String.valueOf(COUNTER.incrementAndGet()));
stompChannel.writeAndFlush(subscribeFrame);
}
});
}
use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.
the class StompChatHandler method transformToMessage.
private static StompFrame transformToMessage(StompFrame sendFrame, StompSubscription subscription) {
StompFrame messageFrame = new DefaultStompFrame(StompCommand.MESSAGE, sendFrame.content().retainedDuplicate());
String id = UUID.randomUUID().toString();
messageFrame.headers().set(MESSAGE_ID, id).set(SUBSCRIPTION, subscription.id()).set(CONTENT_LENGTH, Integer.toString(messageFrame.content().readableBytes()));
CharSequence contentType = sendFrame.headers().get(CONTENT_TYPE);
if (contentType != null) {
messageFrame.headers().set(CONTENT_TYPE, contentType);
}
return messageFrame;
}
use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.
the class StompChatHandler method sendErrorFrame.
private static void sendErrorFrame(String message, String description, ChannelHandlerContext ctx) {
StompFrame errorFrame = new DefaultStompFrame(StompCommand.ERROR);
errorFrame.headers().set(MESSAGE, message);
if (description != null) {
errorFrame.content().writeCharSequence(description, CharsetUtil.UTF_8);
}
ctx.writeAndFlush(errorFrame).addListener(ChannelFutureListener.CLOSE);
}
use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.
the class StompClientHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, StompFrame frame) throws Exception {
String subscrReceiptId = "001";
String disconReceiptId = "002";
switch(frame.command()) {
case CONNECTED:
StompFrame subscribeFrame = new DefaultStompFrame(StompCommand.SUBSCRIBE);
subscribeFrame.headers().set(StompHeaders.DESTINATION, StompClient.TOPIC);
subscribeFrame.headers().set(StompHeaders.RECEIPT, subscrReceiptId);
subscribeFrame.headers().set(StompHeaders.ID, "1");
System.out.println("connected, sending subscribe frame: " + subscribeFrame);
state = ClientState.AUTHENTICATED;
ctx.writeAndFlush(subscribeFrame);
break;
case RECEIPT:
String receiptHeader = frame.headers().getAsString(StompHeaders.RECEIPT_ID);
if (state == ClientState.AUTHENTICATED && receiptHeader.equals(subscrReceiptId)) {
StompFrame msgFrame = new DefaultStompFrame(StompCommand.SEND);
msgFrame.headers().set(StompHeaders.DESTINATION, StompClient.TOPIC);
msgFrame.content().writeBytes("some payload".getBytes());
System.out.println("subscribed, sending message frame: " + msgFrame);
state = ClientState.SUBSCRIBED;
ctx.writeAndFlush(msgFrame);
} else if (state == ClientState.DISCONNECTING && receiptHeader.equals(disconReceiptId)) {
System.out.println("disconnected");
ctx.close();
} else {
throw new IllegalStateException("received: " + frame + ", while internal state is " + state);
}
break;
case MESSAGE:
if (state == ClientState.SUBSCRIBED) {
System.out.println("received frame: " + frame);
StompFrame disconnFrame = new DefaultStompFrame(StompCommand.DISCONNECT);
disconnFrame.headers().set(StompHeaders.RECEIPT, disconReceiptId);
System.out.println("sending disconnect frame: " + disconnFrame);
state = ClientState.DISCONNECTING;
ctx.writeAndFlush(disconnFrame);
}
break;
default:
break;
}
}
use of io.netty.handler.codec.stomp.DefaultStompFrame in project netty by netty.
the class StompEncoderBenchmark method setup.
@Setup(Level.Trial)
public void setup() {
byte[] bytes = new byte[contentLength];
ThreadLocalRandom.current().nextBytes(bytes);
content = Unpooled.wrappedBuffer(bytes);
ByteBuf testContent = Unpooled.unreleasableBuffer(content.asReadOnly());
StompHeadersSubframe headersSubframe = ExampleStompHeadersSubframe.EXAMPLES.get(headersType);
stompFrame = new DefaultStompFrame(headersSubframe.command(), testContent);
stompFrame.headers().setAll(headersSubframe.headers());
stompEncoder = new StompSubframeEncoder();
context = new EmbeddedChannelWriteReleaseHandlerContext(pooledAllocator ? PooledByteBufAllocator.DEFAULT : UnpooledByteBufAllocator.DEFAULT, stompEncoder) {
@Override
protected void handleException(Throwable t) {
handleUnexpectedException(t);
}
};
}
Aggregations