2

我遵循了此处提供的用户指南:我在我的 pom 中添加了这个:

<dependency>
  <groupId>org.glassfish.tyrus</groupId>
  <artifactId>tyrus-server</artifactId>
  <version>1.2</version>
</dependency>
<dependency>
  <groupId>org.glassfish.tyrus</groupId>
  <artifactId>tyrus-container-grizzly</artifactId>
  <version>1.2</version>
</dependency>

我在我的主课上写了这个:

Server server = new Server("localhost", 8624, "/", EchoEndPoint.class);
try 
  {
   server.start();
   BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
   System.out.print("Please press a key to stop the server.");
   reader.readLine();
  } 
catch(Exception ex) { ex.printStackTrace(); } 
finally 
  {
   server.stop();
  }

我的 EchoEndPoint 类的内容与指南中描述的相同。

我尝试使用 HTML5 websocket 连接到它:

var ws = new WebSocket("ws://localhost:8624/echo");

看来,浏览器端,它没有连接(它直接调用 onClose 回调)。而且,服务器端,我在控制台中得到了这个:

Grave: Invalid Connection header returned: 'keep-alive'
org.glassfish.tyrus.websockets.HandshakeException: Invalid Connection header returned: 'keep-alive'
    at org.glassfish.tyrus.websockets.HandShake.validate(HandShake.java:254)
    at org.glassfish.tyrus.websockets.HandShake.checkForHeader(HandShake.java:246)
    at org.glassfish.tyrus.websockets.HandShake.<init>(HandShake.java:97)
    at org.glassfish.tyrus.websockets.draft06.HandShake06.<init>(HandShake06.java:63)
    [...]
org.glassfish.grizzly.filterchain.DefaultFilterChain execute
Avertissement: Exception during FilterChain execution
java.lang.ClassCastException: org.glassfish.grizzly.http.HttpContent cannot be cast to org.glassfish.tyrus.websockets.DataFrame
    at org.glassfish.tyrus.container.grizzly.WebSocketFilter.handleWrite(WebSocketFilter.java:330)

如果有任何帮助,我复制浏览器检查器捕获的请求标头:

GET /echo HTTP/1.1
Host: localhost:8624
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:22.0) Gecko/20100101 Firefox/22.0 FirePHP/0.7.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Sec-WebSocket-Version: 13
Origin: null
Sec-WebSocket-Key: yhGPwJ26c5fYEZ5/abvtqw==
x-insight: activate
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket

这是握手问题吗?

编辑:我已经在 Chrome (28.0.1500.72) 中尝试过,它正在工作。也许问题来自 Firefox 在构建标头时?

4

1 回答 1

1

泰鲁斯在抱怨Connection: keep-alive, Upgrade头球。

Firefox 在这里没有做错任何事情。

Tyrus在如何处理 Connection 标头方面过于严格并且没有遵循 WebSocket 规范 ( RFC-6455 )。

RFC 在第 4.1 节中指出:

   6.   The request MUST contain a |Connection| header field whose value
        MUST include the "Upgrade" token.

   3.  If the response lacks a |Connection| header field or the
       |Connection| header field doesn't contain a token that is an
       ASCII case-insensitive match for the value "Upgrade", the client
       MUST _Fail the WebSocket Connection_.

这似乎是 Tyrus 中的一个错误。

于 2013-07-31T19:37:10.347 回答