Jump to content

Building Basic Netty Client


AnZaNaMa

Recommended Posts

Hello, I'm working on a mod that communicates information about the minecraft server to outside sources. One of the ways I'm trying to accomplish this is by hooking into certain events and sending out an HTTP post request with a json payload to a user-defined endpoint. I'm using Netty to accomplish this, since it's already used by minecraft. For some reason, though, my HttpClient won't send the messages through. I have print statements before and after message sending and it gets to the first message, but never gets to the second one. I realize this is more of a question about Netty than forge but the Netty community doesn't seem to be very active and I haven't gotten a response yet. I'm hoping that someone here will  be able to help me with this issue.

 

Below, you can find my code:

package com.anzanama.statusapi.http.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

public class HttpClient {
    private String uri;
    private int port;
    private String payload;

    public HttpClient(String uri, int port, String payload) {
        this.uri = uri;
        this.port = port;
        this.payload = payload;
    }

    public void run() throws Exception {
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup);
            b.channel(NioSocketChannel.class);
            //b.option(ChannelOption.SO_KEEPALIVE, true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new HttpClientCodec());
                    ch.pipeline().addLast(new HttpObjectAggregator(1048576));
                    ch.pipeline().addLast(new HttpClientHandler());
                    ch.pipeline().addLast(new HttpRequestEncoder());
                }
            });

            HttpRequest request = HttpRequester.createRequest(this.uri, this.payload);
            request.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json");
            System.out.println("Sending message!");
            Channel f = b.connect(uri, port).channel();
            f.writeAndFlush(request);
            f.closeFuture().sync();
            System.out.println("Message was sent!");
        } finally {
            workerGroup.shutdownGracefully();
        }
    }
}
package com.anzanama.statusapi.http.client;

import io.netty.handler.codec.http.*;

import static io.netty.buffer.Unpooled.copiedBuffer;

public class HttpRequester {
    public static FullHttpRequest createRequest(String uri, String payload) {
        return createRequest(uri, payload.getBytes());
    }

    public static FullHttpRequest createRequest(String uri, byte[] payload) {
        return createRequest(uri, payload, HttpVersion.HTTP_1_1);
    }

    public static FullHttpRequest createRequest(String uri, byte[] payload, final HttpVersion version) {
        if (0 < payload.length)
        {
            FullHttpRequest request = new DefaultFullHttpRequest(version, HttpMethod.POST, uri, copiedBuffer(payload));
            request.headers().set(HttpHeaderNames.CONTENT_LENGTH, payload.length);
            return request;
        }
        else
        {
            return new DefaultFullHttpRequest(version, HttpMethod.POST, uri);
        }
    }
}

 

- Just because things are the way they are doesn't mean they can't be the way you want them to be. Unless they're aspen trees. You can tell they're aspens 'cause the way they are.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.