尝试连接Scalecube的Socket.IO Java Server时收到400错误请求

时间:2017-05-10 14:08:43

标签: javascript java sockets socket.io

我正在使用Scalecube的Socket.IO Java Server,如他们的documentation所述,将我的java后端的消息发送到侦听套接字的前端的javascript代码。不幸的是,我无法完成套接字连接。我正在localhost:5000上的套接字中连续收到400 Bad Request。

我做错了什么?

前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Socket.IO</title>
    <script src="socket.io.js"></script>
    <script>
        var socket = io('http://localhost:5000');
        socket.on('connect', function(){
            console.log('connect');
        });
        socket.on('event', function(data){
            console.log(data);
        });
        socket.on('disconnect', function(){
            console.log('disconnect');
        });
    </script>
</head>
<body>
</body>
</html>

后端

package test;

import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import io.scalecube.socketio.Session;
import io.scalecube.socketio.SocketIOAdapter;
import io.scalecube.socketio.SocketIOServer;
import org.apache.log4j.BasicConfigurator;

public class Test {
    public static void main(String [] args) {
        BasicConfigurator.configure();

        SocketIOServer echoServer = SocketIOServer.newInstance(5000);
        echoServer.setListener(new SocketIOAdapter() {
            @Override
            public void onConnect(Session session) {
                System.out.println("Connected: " + session);
            }

            @Override
            public void onMessage(Session session, ByteBuf message) {
                System.out.println("Received: " + message.toString(CharsetUtil.UTF_8));
                message.release();
            }

            @Override
            public void onDisconnect(Session session) {
                System.out.println("Disconnected: " + session);
            }
        });
        echoServer.start();
    }
}

的pom.xml

<project>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>1.0.0</version>
    <modelVersion>4.0.0</modelVersion>

    <dependencies>
        <dependency>
            <groupId>io.scalecube</groupId>
            <artifactId>socketio</artifactId>
            <version>2.3.4</version>
        </dependency>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-buffer</artifactId>
            <version>4.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-common</artifactId>
            <version>4.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-handler</artifactId>
            <version>4.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-codec</artifactId>
            <version>4.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-codec-http</artifactId>
            <version>4.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-transport</artifactId>
            <version>4.1.6.Final</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-transport-native-epoll</artifactId>
            <version>4.1.6.Final</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.5</version>
        </dependency>
    </dependencies>
</project>

记录和错误消息

相关错误出现在最后几行。

0 [main] INFO io.scalecube.socketio.SocketIOServer  - Socket.IO server starting
35 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory  - Using SLF4J as the default logging framework
81 [main] DEBUG io.netty.util.ResourceLeakDetector  - -Dio.netty.leakDetection.level: simple
81 [main] DEBUG io.netty.util.ResourceLeakDetector  - -Dio.netty.leakDetection.maxRecords: 4
133 [main] DEBUG io.netty.util.internal.PlatformDependent0  - java.nio.Buffer.address: available
135 [main] DEBUG io.netty.util.internal.PlatformDependent0  - sun.misc.Unsafe.theUnsafe: available
137 [main] DEBUG io.netty.util.internal.PlatformDependent0  - sun.misc.Unsafe.copyMemory: available
137 [main] DEBUG io.netty.util.internal.PlatformDependent0  - direct buffer constructor: available
139 [main] DEBUG io.netty.util.internal.PlatformDependent0  - java.nio.Bits.unaligned: available, true
139 [main] DEBUG io.netty.util.internal.PlatformDependent0  - java.nio.DirectByteBuffer.<init>(long, int): available
141 [main] DEBUG io.netty.util.internal.Cleaner0  - java.nio.ByteBuffer.cleaner(): available
142 [main] DEBUG io.netty.util.internal.PlatformDependent  - Java version: 8
143 [main] DEBUG io.netty.util.internal.PlatformDependent  - -Dio.netty.noUnsafe: false
143 [main] DEBUG io.netty.util.internal.PlatformDependent  - sun.misc.Unsafe: available
143 [main] DEBUG io.netty.util.internal.PlatformDependent  - -Dio.netty.noJavassist: false
144 [main] DEBUG io.netty.util.internal.PlatformDependent  - Javassist: unavailable
144 [main] DEBUG io.netty.util.internal.PlatformDependent  - You don't have Javassist in your class path or you don't have enough permission to load dynamically generated classes.  Please check the configuration for better performance.
144 [main] DEBUG io.netty.util.internal.PlatformDependent  - -Dio.netty.tmpdir: /var/folders/gf/nk5vq4s13js9l91p8414w4x80000gr/T (java.io.tmpdir)
145 [main] DEBUG io.netty.util.internal.PlatformDependent  - -Dio.netty.bitMode: 64 (sun.arch.data.model)
145 [main] DEBUG io.netty.util.internal.PlatformDependent  - -Dio.netty.noPreferDirect: false
145 [main] DEBUG io.netty.util.internal.PlatformDependent  - io.netty.maxDirectMemory: 1908932608 bytes
146 [main] DEBUG io.netty.util.ResourceLeakDetectorFactory  - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@1b9e1916
220 [main] DEBUG io.netty.util.internal.PlatformDependent  - org.jctools-core.MpscChunkedArrayQueue: available
252 [main] WARN io.scalecube.socketio.DefaultServerBootstrapFactory  - Env doesn't support epoll transport
272 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup  - -Dio.netty.eventLoopThreads: 8
427 [main] DEBUG io.netty.channel.nio.NioEventLoop  - -Dio.netty.noKeySetOptimization: false
428 [main] DEBUG io.netty.channel.nio.NioEventLoop  - -Dio.netty.selectorAutoRebuildThreshold: 512
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.numHeapArenas: 8
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.numDirectArenas: 8
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.pageSize: 8192
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.maxOrder: 11
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.chunkSize: 16777216
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.tinyCacheSize: 512
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.smallCacheSize: 256
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.normalCacheSize: 64
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
488 [main] DEBUG io.netty.buffer.PooledByteBufAllocator  - -Dio.netty.allocator.cacheTrimInterval: 8192
530 [main] DEBUG io.netty.buffer.ByteBufUtil  - -Dio.netty.allocator.type: pooled
531 [main] DEBUG io.netty.buffer.ByteBufUtil  - -Dio.netty.threadLocalDirectBufferSize: 65536
531 [main] DEBUG io.netty.buffer.ByteBufUtil  - -Dio.netty.maxThreadLocalCharBufferSize: 16384
541 [main] DEBUG io.netty.buffer.AbstractByteBuf  - -Dio.netty.buffer.bytebuf.checkAccessible: true
542 [main] DEBUG io.netty.util.ResourceLeakDetectorFactory  - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@880ec60
638 [main] DEBUG io.netty.channel.DefaultChannelId  - -Dio.netty.processId: 23090 (auto-detected)
642 [main] DEBUG io.netty.util.NetUtil  - -Djava.net.preferIPv4Stack: false
643 [main] DEBUG io.netty.util.NetUtil  - -Djava.net.preferIPv6Addresses: false
644 [main] DEBUG io.netty.util.NetUtil  - Loopback interface: lo0 (lo0, 0:0:0:0:0:0:0:1)
644 [main] DEBUG io.netty.util.NetUtil  - /proc/sys/net/core/somaxconn: 128 (non-existent)
683 [main] DEBUG io.netty.channel.DefaultChannelId  - -Dio.netty.machineId: 9c:f3:87:ff:fe:d6:8b:b6 (auto-detected)
683 [main] DEBUG io.netty.util.internal.ThreadLocalRandom  - -Dio.netty.initialSeedUniquifier: 0xe4fdf6d1b06bd83e
790 [main] INFO io.scalecube.socketio.SocketIOServer  - Socket.IO server started: ServerConfiguration{port=5000, ssl=false, heartbeatTimeout=60, heartbeatInterval=25, closeTimeout=60, transports='websocket,flashsocket,xhr-polling,jsonp-polling', alwaysSecureWebSocketLocation=false, remoteAddressHeader=null, eventExecutorEnabled=true, eventExecutorThreadNumber=8, maxWebSocketFrameSize=65536, epollEnabled=true, httpCompressionEnabled=false, websocketCompressionEnabled=false}
24863 [socketio-io-3-1] DEBUG io.netty.util.Recycler  - -Dio.netty.recycler.maxCapacityPerThread: 32768
24864 [socketio-io-3-1] DEBUG io.netty.util.Recycler  - -Dio.netty.recycler.maxSharedCapacityFactor: 2
24864 [socketio-io-3-1] DEBUG io.netty.util.Recycler  - -Dio.netty.recycler.linkCapacity: 16
24864 [socketio-io-3-1] DEBUG io.netty.util.Recycler  - -Dio.netty.recycler.ratio: 8
24877 [defaultEventExecutorGroup-4-1] DEBUG io.scalecube.socketio.pipeline.PacketDispatcherHandler  - Channel active: [id: 0x3268fa6e, L:/0:0:0:0:0:0:0:1:5000 - R:/0:0:0:0:0:0:0:1:65515]
24901 [socketio-io-3-1] WARN io.scalecube.socketio.pipeline.HandshakeHandler  - Received HTTP bad request: GET /socket.io/ from channel: [id: 0x3268fa6e, L:/0:0:0:0:0:0:0:1:5000 - R:/0:0:0:0:0:0:0:1:65515]
24916 [defaultEventExecutorGroup-4-1] DEBUG io.scalecube.socketio.pipeline.PacketDispatcherHandler  - Channel inactive: [id: 0x3268fa6e, L:/0:0:0:0:0:0:0:1:5000 ! R:/0:0:0:0:0:0:0:1:65515]
26411 [defaultEventExecutorGroup-4-2] DEBUG io.scalecube.socketio.pipeline.PacketDispatcherHandler  - Channel active: [id: 0x8e67ba8d, L:/0:0:0:0:0:0:0:1:5000 - R:/0:0:0:0:0:0:0:1:65524]
26412 [socketio-io-3-2] WARN io.scalecube.socketio.pipeline.HandshakeHandler  - Received HTTP bad request: GET /socket.io/ from channel: [id: 0x8e67ba8d, L:/0:0:0:0:0:0:0:1:5000 - R:/0:0:0:0:0:0:0:1:65524]
26415 [defaultEventExecutorGroup-4-2] DEBUG io.scalecube.socketio.pipeline.PacketDispatcherHandler  - Channel inactive: [id: 0x8e67ba8d, L:/0:0:0:0:0:0:0:1:5000 ! R:/0:0:0:0:0:0:0:1:65524]
28951 [defaultEventExecutorGroup-4-3] DEBUG io.scalecube.socketio.pipeline.PacketDispatcherHandler  - Channel active: [id: 0x874367ca, L:/0:0:0:0:0:0:0:1:5000 - R:/0:0:0:0:0:0:0:1:65533]
28953 [socketio-io-3-3] WARN io.scalecube.socketio.pipeline.HandshakeHandler  - Received HTTP bad request: GET /socket.io/ from channel: [id: 0x874367ca, L:/0:0:0:0:0:0:0:1:5000 - R:/0:0:0:0:0:0:0:1:65533]
28955 [defaultEventExecutorGroup-4-3] DEBUG io.scalecube.socketio.pipeline.PacketDispatcherHandler  - Channel inactive: [id: 0x874367ca, L:/0:0:0:0:0:0:0:1:5000 ! R:/0:0:0:0:0:0:0:1:65533]

Socket.io: 2.0.1 Java: 1.8.0_91

1 个答案:

答案 0 :(得分:0)

我使用netty-socketio library解决了这个问题。上面提到的库似乎不能正常工作。

<dependency>
    <groupId>com.corundumstudio.socketio</groupId>
    <artifactId>netty-socketio</artifactId>
    <version>1.7.12</version>
</dependency>