民间!
我正在使用WebSocket实现聊天Web应用程序,但是,当在Jetty 9.1.3.v20140225上运行时,在建立连接之后,从服务器端发送的一些消息不会在客户端上被接收侧。奇怪的是,这种不良行为发生在一些消息上,但其他消息都没问题(最后请参阅我的PS)。
我已经尝试过使用Chrome 33(我知道使用的是通用扩展扩展)和FireFox 28(不会这样做)和类似的结果。事实上,Chrome会收到一些消息,但FireFox不会收到任何消息。
当应用程序在TomCat 7.0.47上运行时它运行正常,所以我怀疑问题出在Jetty ......
故事告诉我们看看我的最小应用程序的代码,它可以重现问题:
首先,有一个简单的页面,您可以通过点击相应的按钮来连接,发送消息(并接收响应),并断开WebSocket。
hello.html的:
<html>
<head>
<meta charset="ISO-8859-1">
<title>Minimum - WebSocket Sandbox</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#connect").click(function() {
connect();
});
$("#send").click(function() {
send("John Doe");
});
$("#disconnect").click(function() {
disconnect();
});
});
</script>
<script>
var wsocket;
function connect() {
var base = getAppLocation("ws:");
wsocket = new WebSocket(base + "/hello");
wsocket.onmessage = function(e) {
log("<<< " + e.data);
};
wsocket.onopen = function() {
log("Connection opened.");
};
wsocket.onclose = function(e) {
log("Connection closed. Reason: (" + e.code + ") " + e.reason);
};
wsocket.onerror = function(e) {
log("Error!");
};
}
function disconnect() {
wsocket.close(1000, "Closed by user.");
wsocket = null;
}
function send(msg) {
if ( !wsocket || wsocket.readyState != WebSocket.OPEN ) {
log("Not connected.");
return;
}
wsocket.send(msg);
log(">>> " + msg);
}
function log(msg) {
$("#output").append(msg + "<br/>");
}
/**
* This just ajust WebSocket URL ocording to the server serving the page.
*/
function getAppLocation(proto) {
if ( !proto ) {
proto = window.location.protocol;
}
else if ( proto == "ws:" && window.location.protocol == "https:" ) {
proto = "wss:";
}
var host = window.location.host;
var path = window.location.pathname.split('/')[1];
var base = proto + "//" + host + "/" + path;
return base;
}
</script>
</head>
<body>
<div>
<button id="connect">Connect</button>
<button id="send">Send</button>
<button id="disconnect">Disconnect</button>
</div>
<div id="output"></div>
</body>
</html>
在服务器端,有一个端点在连接打开后立即向客户端发送消息(注释掉的行显示Chrome客户端收到的示例消息)。它还回应从客户端收到的消息。
HelloEndPoint:
package com.ciandt.helenov.sandbox.websocket.min;
import java.io.IOException;
import javax.websocket.EndpointConfig;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/hello")
public class HelloEndpoint {
@OnOpen
public void onOpen ( Session session, EndpointConfig conf ) {
try {
// this is not received in the client
session.getBasicRemote().sendText( "o" );
// this is (but only if client is a Chrome)
// session.getBasicRemote().sendText( "a[\"100\"]" );
}
catch ( IOException e ) {
e.printStackTrace();
}
}
@OnMessage
public void onMessage ( Session session, String msg ) {
try {
session.getBasicRemote().sendText( "Hello, " + msg + "!" );
}
catch ( IOException e ) {
e.printStackTrace();
}
}
}
最后,这个pom.xml打包并运行Web应用程序。我通过Eclipse中的嵌入式maven插件运行Jetty(问题出现的地方)和TomCat(运行正常)
的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>WebSocket Mininum</name>
<description>WebSocket Minimum Web Application</description>
<groupId>com.ciandt.helenov.sandbox</groupId>
<artifactId>ws-min</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
<slf4j.version>1.7.2</slf4j.version>
<logback.version>1.0.13</logback.version>
<jetty.version>9.1.3.v20140225</jetty.version>
<tomcat.version>7.0.47</tomcat.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>${java.version}</compilerVersion>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgument>-proc:none</compilerArgument>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
<!-- Support to use Eclipse -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<!-- Support to run Jetty on Eclipse -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<webAppConfig>
<contextPath>/${project.artifactId}</contextPath>
</webAppConfig>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9090</port>
<path>/${project.artifactId}</path>
<systemProperties>
<appserver.base>${project.build.directory}/tomcat-base</appserver.base>
<appserver.home>${project.build.directory}/tomcat-home</appserver.home>
<derby.system.home>${project.build.directory}/tomcat-base/logs</derby.system.home>
<java.io.tmpdir>${project.build.directory}</java.io.tmpdir>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
这就是全部!
在Jetty上运行应用程序时(通过调用mvn jetty:run),我要求连接WebSocket(通过单击页面上的“连接”按钮),端点中的onOpen()方法运行并将消息发送回客户端但是因为我预计会显示它,所以没有收到。在TomCat上运行相同的应用程序时(通过调用mvn tomcat7:run),它运行良好。
有人有过类似的问题吗?任何人都知道发生了什么?可以是Jetty服务器上的错误吗?
TIA和问候!
Heleno的
PS:我打算在我的应用程序中使用Spring和SockJS,但SockJS在服务器上发送&#34; open frame&#34; (只是一个&#34; o&#34;)刚刚打开连接后,但由于没有收到它,它会回退放弃WebSocket连接并转向XHR Streaming。这就是我想要避免的。但是,由于我设法在没有Spring和SockJS的情况下重现问题(如所提供的代码中所示),我已经解除了他们对此问题的任何参与。