Search in sources :

Example 1 with SockJsMessage

use of com.cgbystrom.sockjs.SockJsMessage in project sockjs-netty by cgbystrom.

the class XhrSendTransport method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    HttpRequest request = (HttpRequest) e.getMessage();
    if (request.getContent().readableBytes() == 0) {
        BaseTransport.respond(e.getChannel(), INTERNAL_SERVER_ERROR, "Payload expected.");
        return;
    }
    transportMetrics.messagesReceived.mark();
    transportMetrics.messagesReceivedSize.update(request.getContent().readableBytes());
    //logger.debug("Received {}", request.getContent().toString(CharsetUtil.UTF_8));
    String contentTypeHeader = request.getHeader(CONTENT_TYPE);
    if (contentTypeHeader == null) {
        contentTypeHeader = BaseTransport.CONTENT_TYPE_PLAIN;
    }
    String decodedContent;
    if (BaseTransport.CONTENT_TYPE_FORM.equals(contentTypeHeader)) {
        QueryStringDecoder decoder = new QueryStringDecoder("?" + request.getContent().toString(CharsetUtil.UTF_8));
        List<String> d = decoder.getParameters().get("d");
        if (d == null) {
            BaseTransport.respond(e.getChannel(), INTERNAL_SERVER_ERROR, "Payload expected.");
            return;
        }
        decodedContent = d.get(0);
    } else {
        decodedContent = request.getContent().toString(CharsetUtil.UTF_8);
    }
    if (decodedContent.length() == 0) {
        BaseTransport.respond(e.getChannel(), INTERNAL_SERVER_ERROR, "Payload expected.");
        return;
    }
    String[] messages = MAPPER.readValue(decodedContent, String[].class);
    for (String message : messages) {
        SockJsMessage jsMessage = new SockJsMessage(message);
        ctx.sendUpstream(new UpstreamMessageEvent(e.getChannel(), jsMessage, e.getRemoteAddress()));
    }
    if (isJsonpEnabled) {
        BaseTransport.respond(e.getChannel(), OK, "ok");
    } else {
        BaseTransport.respond(e.getChannel(), NO_CONTENT, "");
    }
}
Also used : SockJsMessage(com.cgbystrom.sockjs.SockJsMessage)

Example 2 with SockJsMessage

use of com.cgbystrom.sockjs.SockJsMessage in project sockjs-netty by cgbystrom.

the class WebSocketClient method send.

@Override
public void send(String message) {
    ChannelBuffer cb = Frame.messageFrame(new SockJsMessage(message)).getData();
    // Skip the framing char
    cb.readerIndex(1);
    channel.write(new TextWebSocketFrame(cb));
}
Also used : SockJsMessage(com.cgbystrom.sockjs.SockJsMessage) TextWebSocketFrame(org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

SockJsMessage (com.cgbystrom.sockjs.SockJsMessage)2 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1 TextWebSocketFrame (org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame)1