Search in sources :

Example 1 with TextWebSocketFrame

use of org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame in project sockjs-netty by cgbystrom.

the class WebSocketClient method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    if (!wsHandshaker.isHandshakeComplete()) {
        wsHandshaker.finishHandshake(e.getChannel(), (HttpResponse) e.getMessage());
        callback.onOpen(WebSocketClient.this);
        return;
    }
    if (e.getMessage() instanceof TextWebSocketFrame) {
        TextWebSocketFrame wf = (TextWebSocketFrame) e.getMessage();
        String frame = wf.getText();
        char frameType = frame.charAt(0);
        if (!sockJsHandshakeDone) {
            if (frameType == 'o') {
                sockJsHandshakeDone = true;
            } else {
                throw new IllegalStateException("Expected open frame 'o' as first frame. Got " + frame);
            }
            return;
        }
        switch(frameType) {
            case 'h':
                break;
            case 'a':
                try {
                    List<String> messages = objectMapper.readValue(frame.substring(1), List.class);
                    for (String msg : messages) {
                        try {
                            callback.onMessage(msg);
                        } catch (Exception ex) {
                            callback.onError(ex);
                        }
                    }
                } catch (Exception ex) {
                    throw new IllegalArgumentException("Unable to decode frame: " + frame);
                }
                break;
            case 'c':
                disconnect();
                break;
            default:
                throw new IllegalArgumentException("Received unknown frame type '" + frameType + "'");
        }
    }
}
Also used : TextWebSocketFrame(org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame) URISyntaxException(java.net.URISyntaxException)

Example 2 with TextWebSocketFrame

use of org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame 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)

Example 3 with TextWebSocketFrame

use of org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame in project load-balancer by RestComm.

the class HttpRequestHandler method handleWebSocketFrame.

private void handleWebSocketFrame(ChannelHandlerContext ctx, final MessageEvent e) {
    Object msg = e.getMessage();
    final String request = ((TextWebSocketFrame) msg).getText();
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Channel %s received WebSocket request %s", ctx.getChannel().getId(), request));
    }
    // Modify the Client Pipeline - Phase 2
    Channel channel = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel())).getChannel();
    ChannelPipeline p = channel.getPipeline();
    websocketServerPipelineFactory.upgradeClientPipelineFactoryPhase2(p, wsVersion);
    if (channel != null) {
        channel.write(new TextWebSocketFrame(request));
    }
}
Also used : Channel(org.jboss.netty.channel.Channel) TextWebSocketFrame(org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame) JsonObject(com.google.gson.JsonObject) StatisticObject(org.mobicents.tools.sip.balancer.StatisticObject) NodesInfoObject(org.mobicents.tools.sip.balancer.NodesInfoObject) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline)

Example 4 with TextWebSocketFrame

use of org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame in project load-balancer by RestComm.

the class HttpResponseHandler method handleWebSocketFrame.

private void handleWebSocketFrame(ChannelHandlerContext ctx, MessageEvent e) {
    Object msg = e.getMessage();
    final String response = ((TextWebSocketFrame) msg).getText();
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Channel %s received WebSocket response %s", ctx.getChannel().getId(), response));
    }
    Channel channel = HttpChannelAssociations.channels.get(new AdvancedChannel(e.getChannel())).getChannel();
    if (channel != null) {
        channel.write(new TextWebSocketFrame(response));
    }
}
Also used : Channel(org.jboss.netty.channel.Channel) TextWebSocketFrame(org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame)

Aggregations

TextWebSocketFrame (org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame)4 Channel (org.jboss.netty.channel.Channel)2 SockJsMessage (com.cgbystrom.sockjs.SockJsMessage)1 JsonObject (com.google.gson.JsonObject)1 URISyntaxException (java.net.URISyntaxException)1 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)1 NodesInfoObject (org.mobicents.tools.sip.balancer.NodesInfoObject)1 StatisticObject (org.mobicents.tools.sip.balancer.StatisticObject)1