websocket——Firefox 无法建立到 ws://... 服务器的连接

java 同时被 2 个专栏收录
87 篇文章 1 订阅
3 篇文章 0 订阅

页面访问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后异常消失

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Testing websocket</title>
</head>
<body>
	<div>
		<input type="submit" value="Start" οnclick="start()" />
		<input type="button" value="Stop" οnclick="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>

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");
	}
	
}



  • 1
    点赞
  • 10
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值