不知道是路径的问题,还是ssh框架会拦截websocket请求?总是访问不到我注解的路径,还请大神指点
前端代码
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'mychat.jsp' starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <script type="text/javascript" src="js/jquery-1.8.3.js"></script> 20 <!-- 21 <link rel="stylesheet" type="text/css" href="styles.css"> 22 --> 23 <script type="text/javascript"> 24 25 var webSocket = null; 26 27 function init() 28 { 29 webSocket = new WebSocket("ws://127.0.0.1:9090/Einvest/java/socket"); //建立连接点 Einvest是我的项目名 /java/socket是我的注解路径 没加包名 31 webSocket.onerror = function(event) 32 { 33 alert("initonerror"); 34 onError(event); 35 }; 36 37 webSocket.onopen = function(event) 38 { 39 alert("initonopen"); 40 onOpen(event); 41 }; 42 43 webSocket.onmessage = function(event) //接受客户端消息 44 { 45 alert("initonmessage"); 46 onMessage(event); 47 }; 48 } 49 50 function onMessage(event) 51 { 52 alert("functiononMessage"); 53 document.getElementById('messages').innerHTML += '<br />' + event.data; 54 } 55 56 function onOpen(event) 57 { 58 alert("functiononOpen"); 59 document.getElementById('messages').innerHTML = 'Connection established'; 60 61 webSocket.send("ds"); //发送消息给服务器端 62 } 63 64 function onError(event) 65 { 66 alert("functiononError"); 67 alert(event.data); 68 } 69 70 function start() 71 { 72 alert("start"); 73 init(); 74 } 75 /* $(function(){ 76 alert("socket"); 77 init(); 78 }); */ 79 </script> 80 </head> 81 82 <body> 83 <button onclick="start()">连接</button> 84 <div id="messages"></div> 85 </body> 86 </html>
后台代码
1 package Chat; 2 3 import javax.websocket.OnClose; 4 import javax.websocket.OnMessage; 5 import javax.websocket.OnOpen; 6 import javax.websocket.Session; 7 import javax.websocket.server.ServerEndpoint; 8 9 @ServerEndpoint("/java/socket") 10 public class TalkWebSocketServer { 11 @OnMessage //接受客户端消息 12 public void onMessage(String msg, Session session) throws Exception 13 { 14 15 System.out.println("msg : " + msg); 16 System.out.println(session.getQueryString()); 17 String tmp = "让我们定义一个 Tomcat websocket服务器端"; 18 System.out.println("send msg : " + tmp); 19 session.getBasicRemote().sendText(tmp); //发送信息到客户端 20 session.close(); //关闭连接点 21 } 22 23 @OnOpen //成功连接时执行此代码 24 public void onOpen() 25 { 26 System.out.println("------------onOpen------------"); 27 } 28 29 @OnClose //连接关闭时执行 30 public void onClose() 31 { 32 System.out.println("------------onClose------------"); 33 } 34 }
总是连接不上 报404错误
看到帖子上有同学说是websocket-api.jar包冲突,我是把omcat里面的websocket-api.jar包放到了一个uer-library里面的项目中的lib目录里面是没有websocket-api.jar包的,不知道哪里有问题,还请大神帮助,多谢
webSocket = new WebSocket("ws://127.0.0.1:9090/Einvest/java/socket");这个会被ssh框架拦截吗,导致404错误
你怎么把init() 注释了呢
哦,一开始我是想在页面加载时就去连接的,然后连接不上,所以写了个按钮 <button onclick="start()">连接</button>点击调用start()方法执行init()结果还是不可以
我觉得是路径和后台java代码有问题,刚刚学习,所以还有点不懂,😄