mrniko / netty-socketio

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

Netty-socketio communicate with Object-C NSData problem #287

Closed hyichao closed 8 years ago

hyichao commented 8 years ago

Hi,

Thank you for such a great project of socket.io. While using it, i got some problem. I'm using:

In the server side, i add a binary handler and a direct String handler as follows

        server.addEventListener("string", String.class, new DataListener<String>() {
            @Override
            public void onData(SocketIOClient socketIOClient, String data, AckRequest ackRequest) throws Exception {
                System.out.println("string emit received");
                String str = "This is String Hander";
                server.getBroadcastOperations().sendEvent("string",str);
            }
        });

        server.addEventListener("binary", byte[].class, new DataListener<byte[]>() {
            @Override
            public void onData(SocketIOClient socketIOClient, byte[] buffer, AckRequest ackRequest) throws Exception {
                System.out.println("binary emit received");
                String str = "This is Binary Hander";
                byte[] bt = str.getBytes();
                server.getBroadcastOperations().sendEvent("string", str);
            }
        });

In the client side, i emit as follows

    NSString* str = @"Hello world!";
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding
                     allowLossyConversion:false];
    [self.socket emit:@"string" withItems:@[str]];
    [self.socket emit:@"binary" withItems:@[data]];

and I got error message in the Java Server side

string emit received
[nioEventLoopGroup-3-3] ERROR com.corundumstudio.socketio.handler.InPacketHandler - Error during data processing. Client sessionId: 8098c777-c389-435b-b93d-578f8426d3c2, data: 
java.lang.IllegalStateException: Can't find attachment by index: 0 in packet source
    at com.corundumstudio.socketio.protocol.PacketDecoder.parseBinary(PacketDecoder.java:259)
    at com.corundumstudio.socketio.protocol.PacketDecoder.decode(PacketDecoder.java:166)
    at com.corundumstudio.socketio.protocol.PacketDecoder.decodePackets(PacketDecoder.java:150)
    at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:65)
    at com.corundumstudio.socketio.handler.InPacketHandler.channelRead0(InPacketHandler.java:36)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:187)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
    at com.corundumstudio.socketio.transport.WebSocketTransport.channelRead(WebSocketTransport.java:94)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at com.corundumstudio.socketio.transport.PollingTransport.channelRead(PollingTransport.java:109)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at com.corundumstudio.socketio.handler.AuthorizeHandler.channelRead(AuthorizeHandler.java:115)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:108)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:182)
    at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:308)
    at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:294)
    at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:846)
    at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:130)
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468)
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354)
    at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:116)
    at io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run(DefaultThreadFactory.java:137)
    at java.lang.Thread.run(Thread.java:745)

The message shows that String emit works well, and logging the information proves it. However the binary emit meets difficulties.

So, my Q is, is there any mistake that i have made while sending NSData as binary? All i got to do is to send NSData to the server side, what type of data class should i use in netty-socket.io?

hyichao commented 8 years ago

Finally i found out how to fix it... If you guys are trying to send binary data to netty-socketio as server, please remember to upgrade java from older version to Java1.8, since only Java1.8 supports binary well. Inside the source code, there is an Java native API that prevent older version java to parse binary. I'll close this issue myself

PeterL1n commented 8 years ago

I currently meet the exact same problem in version 1.7.8 and JRE1.8 does not solve the problem

I use the latest socket.io-swift-client. But interestingly, everything is fine on iPhone 4, iPhone 5, but not on iPhone 6 and above. It sounds like a client-side problem, but I can't find any report on the socket.o-swift-client Github site.

Can anyone give me any feedback?

mrniko commented 8 years ago

@PeterL1n could you try it with "master" branch version? There was some issue with packet length parsing https://github.com/mrniko/netty-socketio/commit/53d6bde2c381e648164effe8d49d71c6fcb29172

PeterL1n commented 8 years ago

I am not quite familiar with Github system. What do you mean by master branch? 1.7.8 is not master branch?

Do you want me to directly embed the source code into my project?

mrniko commented 8 years ago

@PeterL1n I mean sync with git@github.com:mrniko/netty-socketio.git build it drop in classpath and try again your case.

PeterL1n commented 8 years ago

So I synced it, and imported as a maven project. Export it to a jar file. But the jar file does not include dependency. Is there any way to combine with Maven and automatically get the dependency?

PeterL1n commented 8 years ago

Ok. Now I got the latest one running, but it still fails.

mrniko commented 8 years ago

@PeterL1n Could you please try it again due to this https://github.com/mrniko/netty-socketio/pull/326 ?

PeterL1n commented 8 years ago

Yes. Version 1.7.10 fixes the problem. Now I can send binary to the server. Big thanks :)

mrniko commented 8 years ago

Perfect!