Tyrus websocket服务器握手问题?

时间:2013-07-30 09:51:40

标签: websocket tyrus

我遵循了可用的用户指南here:我在我的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类的内容与描述的in the guide相同。

我尝试使用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构建标题时的问题?

1 个答案:

答案 0 :(得分:0)

Tyrus抱怨Connection: keep-alive, Upgrade标题。

Firefox在这里做错了什么。

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

RFC在Section 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中的一个错误。