user63898
user63898

Reputation: 30885

How to use netty as WebSocket server and HTTP client both in the same server

Im using Netty as simple websocket server , i will like able netty to comunicate Second netty server using HTTP as client . how can it be done ?
For now my Initializer looks like this :

public class PDServerInitializer extends ChannelInitializer<Channel> {
     private final ChannelGroup group;

        public PDServerInitializer(ChannelGroup group) {
            this.group = group;
        }

        protected void initChannel(Channel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            pipeline.addLast(new HttpServerCodec());
            pipeline.addLast(new HttpObjectAggregator(64 * 1024));
            pipeline.addLast(new ChunkedWriteHandler());
            pipeline.addLast(new HttpRequestHandler("/ws"));
            pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
            pipeline.addLast(new TextWebSocketFrameHandler(group));
        }
}



public class TextWebSocketFrameHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
    private final ChannelGroup group;

    public TextWebSocketFrameHandler(ChannelGroup group) {
        this.group = group;
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {

            ctx.pipeline().remove(HttpRequestHandler.class);



            group.add(ctx.channel());
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }

    @Override
    public void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
        msg.retain();
        TextWebSocketFrame frame = (TextWebSocketFrame) msg;
        String jsonRequest = frame.text(); 

       //I WAHT TO SENT THE STRING jsonRequest to NETTY Server 2 as client from here 

    }
}

The Server side working great i want to sent String "jsonRequest" to second netty as http request and receive the answer from this specific server

Upvotes: 0

Views: 1180

Answers (1)

Irunika Weeraratne
Irunika Weeraratne

Reputation: 21

Http and WebSocket are different protocols. What WebSocket does is using a HTTP request to upgrade it's connection from HTTP to WebSocket. After the handshake netty will not understand HTTP calls but WebSocket frames.

But IMHO if you are using WebSocket you don't have to use any other protocol to achieve back and forth communication between two peers. Because WebSocket channel is a full duplex channel which means you can use same connection between client and server to either send or recieve messages from the other end.

What you have to understand is that there is only a slight different between a WebSocket server and a client. Client is the one who always initiates a connection. But that's it. After that both sides can listen to channel and write WebSocket frames to the channel. So I guess making the connection initiator as the client would solve your problem.
try Netty WebSocket client example for your client which I guess should work for your scenario.

Upvotes: 1

Related Questions