11

我有一个在嵌入式码头(8.1.8.v20121106)中运行的 WebSocket 服务器,并希望使用码头 WebSocketClient 从另一个 Java 应用程序连接到它。这是我使用的测试方法:

@Test
public void testWebSockets() throws Exception {
    logger.debug("START testWebSockets()");
    WebSocketClientFactory factory = new WebSocketClientFactory();
    factory.setBufferSize(4096);
    factory.start();        

    WebSocketClient client = factory.newWebSocketClient();
    client.setMaxIdleTime(30000);
    client.setMaxTextMessageSize(1024);
    client.setProtocol("MyProtocol");                                       

    WebSocket webSocket = new ClientWebSocket();
    String wsUrl = "ws://localhost:8080/websocket";
    String wssUrl = "wss://localhost:8443/websocket";
    Future future = client.open(new URI(wssUrl), webSocket);
    WebSocket.Connection connection = (Connection) future.get(10, TimeUnit.SECONDS);
    assertNotNull("No connection!", connection);
    connection.sendMessage("TestMessage");
    connection.close();
    logger.debug("END testWebSockets()");
}

使用 wsUrl 不是问题,但我无法让它与 wss 一起使用。我认为服务器不是问题,因为我从 javascript 连接 wss 没有问题。我在这个测试中做错了什么?

服务器和客户端都在 ssl 设置上记录“坏记录 MAC”。这是服务器日志的最后一部分:

    2013-01-15 10:26:06,398 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] unwrap OK NEED_TASK consumed=107 produced=0
2013-01-15 10:26:06,399 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_TASK i/o/u=75/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1
,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_TASK filled=69/75 flushed=0/0
2013-01-15 10:26:06,413 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_UNWRAP i/o/u=75/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=
-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_UNWRAP filled=0/75 flushed=0/0
2013-01-15 10:26:06,415 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] unwrap OK NEED_UNWRAP consumed=6 produced=0
2013-01-15 10:26:06,416 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_UNWRAP i/o/u=69/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=
-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_UNWRAP filled=0/69 flushed=0/0
2013-01-15 10:26:06,419 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] unwrap OK NEED_WRAP consumed=69 produced=0
2013-01-15 10:26:06,420 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_WRAP i/o/u=0/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,
b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_WRAP filled=0/0 flushed=0/0
2013-01-15 10:26:06,421 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] wrap OK NEED_WRAP consumed=0 produced=6
2013-01-15 10:26:06,422 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_WRAP i/o/u=0/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,
b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_WRAP filled=0/0 flushed=6/0
2013-01-15 10:26:06,423 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] wrap OK FINISHED consumed=0 produced=69
2013-01-15 10:26:06,424 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NOT_HANDSHAKING i/o/u=0/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0
,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NOT_HANDSHAKING filled=0/0 flushed=69/0
2013-01-15 10:26:06,426 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NOT_HANDSHAKING i/o/u=0/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0
,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NOT_HANDSHAKING filled=0/0 flushed=0/0
2013-01-15 10:26:06,428 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NOT_HANDSHAKING i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s
=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NOT_HANDSHAKING filled=245/245 flushed=0/0
2013-01-15 10:26:06,430 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - SCEP@1dac071{l(/127.0.0.1:65084)<->r(/127.0.0.1:8444),d=true,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=1r}-{SslConnection@9063ac SSL NEED_WRAP
i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}}
javax.net.ssl.SSLException: bad record MAC
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1639)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1607)
        at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:979)
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:884)
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758)
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
        at org.eclipse.jetty.io.nio.SslConnection.unwrap(SslConnection.java:524)
        at org.eclipse.jetty.io.nio.SslConnection.process(SslConnection.java:359)
        at org.eclipse.jetty.io.nio.SslConnection.access$900(SslConnection.java:48)
        at org.eclipse.jetty.io.nio.SslConnection$SslEndPoint.fill(SslConnection.java:666)
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1035)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:280)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:722)
2013-01-15 10:26:06,463 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ChannelEndPoint - close SCEP@1dac071{l(/127.0.0.1:65084)<->r(/127.0.0.1:8444),d=true,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=1!}-{SslConnection@906
3ac SSL NEED_WRAP i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}}
2013-01-15 10:26:06,469 qtp9902580-25 DEBUG org.eclipse.jetty.http.HttpParser -
javax.net.ssl.SSLException: bad record MAC
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1639)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1607)
        at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:979)
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:884)
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758)
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
        at org.eclipse.jetty.io.nio.SslConnection.unwrap(SslConnection.java:524)
        at org.eclipse.jetty.io.nio.SslConnection.process(SslConnection.java:359)
        at org.eclipse.jetty.io.nio.SslConnection.access$900(SslConnection.java:48)
        at org.eclipse.jetty.io.nio.SslConnection$SslEndPoint.fill(SslConnection.java:666)
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1035)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:280)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:722)
2013-01-15 10:26:06,506 qtp9902580-25 DEBUG org.eclipse.jetty.http.HttpParser - HttpParser{s=-14,l=0,c=0}
org.eclipse.jetty.io.EofException
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1041)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:280)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:722)
Caused by: javax.net.ssl.SSLException: bad record MAC
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1639)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1607)
        at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:979)
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:884)
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758)
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
        at org.eclipse.jetty.io.nio.SslConnection.unwrap(SslConnection.java:524)
        at org.eclipse.jetty.io.nio.SslConnection.process(SslConnection.java:359)
        at org.eclipse.jetty.io.nio.SslConnection.access$900(SslConnection.java:48)
        at org.eclipse.jetty.io.nio.SslConnection$SslEndPoint.fill(SslConnection.java:666)
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1035)
        ... 9 more
2013-01-15 10:26:06,470 qtp9902580-23 Selector0 DEBUG org.eclipse.jetty.io.nio - destroyEndPoint SCEP@1dac071{l(null)<->r(0.0.0.0/0.0.0.0:8444),d=true,open=false,ishut=true,oshut=true,rb=false,wb=false,w=true,i=1!}-{SslConnection@9063ac
SSL NEED_WRAP i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}}
2013-01-15 10:26:06,555 qtp9902580-25 DEBUG org.eclipse.jetty.server.AsyncHttpConnection - Disabled read interest while writing response SSL NEED_WRAP i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0
,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0}
2013-01-15 10:26:06,559 qtp9902580-23 Selector0 DEBUG org.eclipse.jetty.server.AbstractHttpConnection - closed AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0
2013-01-15 10:26:06,560 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio - EOF
org.eclipse.jetty.io.EofException
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1041)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:280)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:722)
Caused by: javax.net.ssl.SSLException: bad record MAC
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1639)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1607)
        at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:979)
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:884)
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758)
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
        at org.eclipse.jetty.io.nio.SslConnection.unwrap(SslConnection.java:524)
        at org.eclipse.jetty.io.nio.SslConnection.process(SslConnection.java:359)
        at org.eclipse.jetty.io.nio.SslConnection.access$900(SslConnection.java:48)
        at org.eclipse.jetty.io.nio.SslConnection$SslEndPoint.fill(SslConnection.java:666)
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1035)
        ... 9 more
2013-01-15 10:26:06,623 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ChannelEndPoint - close SCEP@1dac071{l(null)<->r(0.0.0.0/0.0.0.0:8444),d=true,open=false,ishut=true,oshut=true,rb=false,wb=false,w=true,i=1-}-{SslConnection@9063ac SSL
NEED_WRAP i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0}}
4

1 回答 1

2

我也尝试过使用 Jetty 9.0.3,但得到了与 Jetty 相同的异常 - SSL Websocket Client

由于码头 websocket 客户端似乎有其他人没有的问题,我切换到异步 http 客户端,它对我来说运行完美:

@Test
public void testNingWebSockets() throws Exception {
    logger.debug("START testNingWebSockets()");

    String wsUrl = "ws://localhost:8081/websocket";
    String wssUrl = "wss://localhost:8444/websocket";
    AsyncHttpClient client = new AsyncHttpClient();
    WebSocketUpgradeHandler handler = new WebSocketUpgradeHandler.Builder().addWebSocketListener(new ClientWebSocket()).build();
    WebSocket websocket = client.prepareGet(wssUrl).execute(handler).get();
    Assert.assertNotNull("No connection!", websocket);
    logger.debug("Connection made: " + websocket.isOpen());
    websocket.sendMessage("TestMessage".getBytes());
    Thread.sleep(1000);
    websocket.close();
    logger.debug("END testNingWebSockets()");
}
于 2013-05-28T16:15:53.917 回答