mrniko / netty-socketio

Socket.IO server implemented on Java. Realtime java framework
Apache License 2.0
6.82k stars 1.65k forks source link

Extra headers support? #502

Open vsaksonova opened 6 years ago

vsaksonova commented 6 years ago

I'm using netty-socketio on a java server side. A js client is using socketio.

The client can successfully connect and receive events unless it adds extraheaders (using polling) on connection. We want to use headers to pass tokens for authorization. The error is

Request header field my-token is not allowed by Access-Control-Allow-Headers in preflight response.

If I understand correctly, I have to allow these headers on the server, but I cannot find the way. Already thought extra headers are not supported by netty-socketio.

Can anybody help?

jjyyjjyy commented 5 years ago
        SocketIOChannelInitializer initializer = new SocketIOChannelInitializer();
        Field encoderHandlerField = SocketIOChannelInitializer.class.getDeclaredField("encoderHandler");
        encoderHandlerField.setAccessible(true);
        Configuration configuration = server.getConfiguration();
        WxEncoderHandler encodeHandler = new WxEncoderHandler(configuration, new PacketEncoder(configuration, configuration.getJsonSupport()));
        server.setPipelineFactory(initializer);
        server.start();
        encoderHandlerField.set(initializer, encodeHandler);
@Sharable
public class WxEncoderHandler extends EncoderHandler {

    private void sendMessage(HttpMessage msg, Channel channel, ByteBuf out, HttpResponse res, ChannelPromise promise) {
        res.headers().add(HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS, "Origin, Authorization, Cookie, Accept, X-Requested-With, Last-Modified, Content-Type, io");
    // ...
}

Reassign the encoderHandler field of SocketIOChannelInitializer can work, but a little ugly...