firefox使用 websocket 报无法建立到 ws://服务器的连接错误,ie和chrom都可以,求解释。。
1.打开firefox,输入 about:config,进入配置页
2.搜索websocket配置项
3.配置如下图所示
4.修改配置后,需要重新启动Firefox
页面访问java websocket服务时,出现异常,Firefox下的异常是:Firefox 无法建立到 ws://... 服务器的连接;
IE下的异常是:WebSocket Error: Incorrect HTTP response. Status code 404, Not Found
环境是:apache-tomcat-7.0.62、jdk1.7.0_67、@ServerEndpoint("/websocket")这种声明式写法
我的排查步骤:
1、tomcat的JAVA_HOME指向jdk7
2、Firefox参数调整,据说默认是关闭websocket的,但是我的版本是38.0.6,没有关闭
3、web.xml头部改为<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
4、最后发现,是由于我引入了一个javax.websocket-api.jar放到了WEB-INF/lib/下面,可能跟tomcat7的websocket-api.jar有冲突。
去掉javax.websocket-api.jar后异常消失
[html] view plain copy
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Testing websocket</title>
</head>
<body>
<div>
<input type="submit" value="Start" onclick="start()" />
<input type="button" value="Stop" onclick="stop()" />
</div>
<div id="messages"></div>
<script type="text/javascript">
console.log(window.WebSocket);
var webSocket = new WebSocket('ws://127.0.0.1:8088/WebModule/websocket');
webSocket.onerror = function(event) {
onError(event)
};
webSocket.onopen = function(event) {
onOpen(event)
};
/* webSocket.onclose = function(event){
alert("要离开了?");
}; */
webSocket.onmessage = function(event) {
onMessage(event)
};
function onMessage(event) {
document.getElementById('messages').innerHTML += '<br />'
+ event.data;
}
function onOpen(event) {
document.getElementById('messages').innerHTML = 'Connection established';
}
function onError(event) {
alert(event.data);
}
function start() {
webSocket.send('hello');
return false;
}
function stop() {
webSocket.send('bye bye!');
webSocket.close();
}
</script>
</body>
</html>
[java] view plain copy
import java.io.IOException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket")
public class WebSocketTest {
@OnMessage
public void onMessage(String message, Session session) throws IOException, InterruptedException{
// Print the client message for testing purposes
System.out.println("Received: " + message);
// Send the first message to the client
session.getBasicRemote().sendText("This is the first server message");
// Send 3 messages to the client every 5 seconds
int sentMessages = 0;
while(sentMessages < 3){
Thread.sleep(5000);
session.getBasicRemote().sendText("This is an intermediate server message. Count: " + sentMessages);
sentMessages++;
}
// Send a final message to the client
session.getBasicRemote().sendText("This is the last server message");
}
@OnOpen
public void onOpen(){
System.out.println("Client connected");
}
@OnClose
public void onClose(){
System.out.println("Connection closed");
}
}
这是因为火狐浏览器的websocket默认处于关闭状态,您需要在地址栏输入about:config进入控制台后,开启websocket相关项目后方可使用。
希望我的回答对您有所帮助,如有疑问,欢迎继续咨询我们。
websocket不报错,报无法建立到 ws://服务器的连接错误