Search in sources :

Example 1 with Frame

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

the class HtmlFileTransport method writeRequested.

@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (e.getMessage() instanceof Frame) {
        final Frame frame = (Frame) e.getMessage();
        if (headerSent.compareAndSet(false, true)) {
            HttpResponse response = createResponse(CONTENT_TYPE_HTML);
            response.setHeader(CACHE_CONTROL, "no-store, no-cache, must-revalidate, max-age=0");
            // Safari needs at least 1024 bytes to parse the website. Relevant:
            //   http://code.google.com/p/browsersec/wiki/Part2#Survey_of_content_sniffing_behaviors
            int spaces = 1024 - header.readableBytes();
            ChannelBuffer paddedHeader = ChannelBuffers.buffer(1024 + 50);
            paddedHeader.writeBytes(header);
            for (int i = 0; i < spaces + 20; i++) {
                paddedHeader.writeByte(' ');
            }
            paddedHeader.writeByte('\r');
            paddedHeader.writeByte('\n');
            // Opera needs one more new line at the start.
            paddedHeader.writeByte('\r');
            paddedHeader.writeByte('\n');
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), response, e.getRemoteAddress()));
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(paddedHeader), e.getRemoteAddress()));
        }
        final ChannelBuffer frameContent = Frame.encode(frame, false);
        final ChannelBuffer content = ChannelBuffers.dynamicBuffer(frameContent.readableBytes() + 10);
        Frame.escapeJson(frameContent, content);
        ChannelBuffer wrappedContent = ChannelBuffers.wrappedBuffer(PREFIX, content, POSTFIX);
        ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(wrappedContent), e.getRemoteAddress()));
        logResponseSize(e.getChannel(), content);
    } else {
        super.writeRequested(ctx, e);
    }
}
Also used : Frame(com.cgbystrom.sockjs.Frame) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 2 with Frame

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

the class JsonpPollingTransport method writeRequested.

@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (e.getMessage() instanceof Frame) {
        final Frame frame = (Frame) e.getMessage();
        HttpResponse response = createResponse(CONTENT_TYPE_JAVASCRIPT);
        response.setHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
        response.setHeader(HttpHeaders.Names.CACHE_CONTROL, "no-store, no-cache, must-revalidate, max-age=0");
        ChannelBuffer escapedContent = ChannelBuffers.dynamicBuffer();
        Frame.escapeJson(Frame.encode(frame, false), escapedContent);
        String m = jsonpCallback + "(\"" + escapedContent.toString(CharsetUtil.UTF_8) + "\");\r\n";
        e.getFuture().addListener(ChannelFutureListener.CLOSE);
        final ChannelBuffer content = ChannelBuffers.copiedBuffer(m, CharsetUtil.UTF_8);
        response.setContent(content);
        response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, content.readableBytes());
        ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), response, e.getRemoteAddress()));
        transportMetrics.messagesSent.mark();
        transportMetrics.messagesSentSize.update(content.readableBytes());
    } else {
        super.writeRequested(ctx, e);
    }
}
Also used : Frame(com.cgbystrom.sockjs.Frame) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 3 with Frame

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

the class EventSourceTransport method writeRequested.

@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (e.getMessage() instanceof Frame) {
        Frame frame = (Frame) e.getMessage();
        if (headerSent.compareAndSet(false, true)) {
            HttpResponse response = createResponse(CONTENT_TYPE_EVENT_STREAM);
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), response, e.getRemoteAddress()));
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(NEW_LINE), e.getRemoteAddress()));
        }
        ChannelBuffer wrappedContent = ChannelBuffers.wrappedBuffer(FRAME_BEGIN, Frame.encode(frame, false), FRAME_END);
        ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(wrappedContent), e.getRemoteAddress()));
        logResponseSize(e.getChannel(), wrappedContent);
    } else {
        super.writeRequested(ctx, e);
    }
}
Also used : Frame(com.cgbystrom.sockjs.Frame) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Example 4 with Frame

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

the class XhrStreamingTransport method writeRequested.

@Override
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (e.getMessage() instanceof Frame) {
        if (headerSent.compareAndSet(false, true)) {
            HttpResponse response = createResponse(CONTENT_TYPE_JAVASCRIPT);
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), Channels.future(e.getChannel()), response, e.getRemoteAddress()));
            // IE requires 2KB prefix:
            // http://blogs.msdn.com/b/ieinternals/archive/2010/04/06/comet-streaming-in-internet-explorer-with-xmlhttprequest-and-xdomainrequest.aspx
            DefaultHttpChunk message = new DefaultHttpChunk(Frame.encode(Frame.preludeFrame(), true));
            ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), Channels.future(e.getChannel()), message, e.getRemoteAddress()));
        }
        final Frame frame = (Frame) e.getMessage();
        ChannelBuffer content = Frame.encode(frame, true);
        if (frame instanceof Frame.CloseFrame) {
            e.getFuture().addListener(ChannelFutureListener.CLOSE);
        }
        ctx.sendDownstream(new DownstreamMessageEvent(e.getChannel(), e.getFuture(), new DefaultHttpChunk(content), e.getRemoteAddress()));
        logResponseSize(e.getChannel(), content);
    } else {
        super.writeRequested(ctx, e);
    }
}
Also used : Frame(com.cgbystrom.sockjs.Frame) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer)

Aggregations

Frame (com.cgbystrom.sockjs.Frame)4 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)4