前端代码:
<!DOCTYPE html> <html> <head> <title>测试</title> </head> <body> Welcome<br/><input id="text" type="text"/> <button onclick="send()">发送消息</button> <hr/> <button onclick="closeWebSocket()">关闭WebSocket连接</button> <hr/> <div id="message"></div> </body> <script type="text/javascript"> var websocket = null; //判断当前浏览器是否支持WebSocket if ('WebSocket' in window) { websocket = new WebSocket("ws://localhost:82/websocket/t.ws"); } else { alert('当前浏览器 Not support websocket') } //连接发生错误的回调方法 websocket.onerror = function () { setMessageInnerHTML("WebSocket连接发生错误"); }; //连接成功建立的回调方法 websocket.onopen = function () { setMessageInnerHTML("WebSocket连接成功"); } //接收到消息的回调方法 websocket.onmessage = function (event) { setMessageInnerHTML(event.data); } //连接关闭的回调方法 websocket.onclose = function () { setMessageInnerHTML("WebSocket连接关闭"); } //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。 window.onbeforeunload = function () { closeWebSocket(); } //将消息显示在网页上 function setMessageInnerHTML(innerHTML) { document.getElementById('message').innerHTML += innerHTML + '<br/>'; } //关闭WebSocket连接 function closeWebSocket() { websocket.close(); } //发送消息 function send() { var message = document.getElementById('text').value; websocket.send(message); } </script> </html>
后端配置:
package com.common.config; import java.util.regex.Pattern; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.jfinal.handler.Handler; import com.jfinal.kit.StrKit; public class WebSocketHandler extends Handler { private Pattern filterUrlRegxPattern; public WebSocketHandler(String filterUrlRegx) { if (StrKit.isBlank(filterUrlRegx)) throw new IllegalArgumentException("The para filterUrlRegx can not be blank."); filterUrlRegxPattern = Pattern.compile(filterUrlRegx); } @Override public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) { if (filterUrlRegxPattern.matcher(target).find()) return ; else next.handle(target, request, response, isHandled); } }
main-configHandler配置: public void configHandler(Handlers me) { me.add(new WebSocketHandler("^/websocket")); }
主要的接口:
@ServerEndpoint("/websocket/t.ws") public class WebsocketTest { }
刷新页面控制台的debug日志:
2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-41 Selector0]-[org.eclipse.jetty.io.nio.SelectorManager$SelectSet.createEndPoint()]: created SCEP@ade7c1f{l(/192.168.1.120:56220)<->r(/192.168.1.120:82),d=false,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{AsyncHttpConnection@5652178b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43]-[org.eclipse.jetty.http.HttpParser.parseNext()]: filled 500/500 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.server.Server.handle()]: REQUEST /websocket/t.ws on AsyncHttpConnection@5652178b,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=42,c=0},r=1 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.server.handler.ContextHandler.doScope()]: scope null||/websocket/t.ws @ o.e.j.w.WebAppContext{/,file:/E:/dyry/medicaltriagesystem_olddy/src/main/webapp/} 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.server.handler.ContextHandler.doScope()]: context=||/websocket/t.ws @ o.e.j.w.WebAppContext{/,file:/E:/dyry/medicaltriagesystem_olddy/src/main/webapp/} 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.server.session.SessionHandler.doScope()]: sessionManager=org.eclipse.jetty.server.session.HashSessionManager@27f981c6 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.server.session.SessionHandler.doScope()]: session=null 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.servlet.ServletHandler.doScope()]: servlet |/websocket/t.ws|null -> default 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.servlet.ServletHandler.doHandle()]: chain=shiro->jfinal->default 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter()]: call filter shiro 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.apache.shiro.session.mgt.DefaultSessionManager.retrieveSession()]: Unable to resolve session ID from SessionKey [org.apache.shiro.web.session.mgt.WebSessionKey@366bc7c0]. Returning null to indicate a session could not be found. 2019-07-18 11:42:28 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter()]: call filter jfinal 2019-07-18 11:42:30 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter()]: call servlet default 2019-07-18 11:42:30 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.servlet.DefaultServlet.getResource()]: Resource /websocket/t.ws.gz=null 2019-07-18 11:42:30 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.servlet.DefaultServlet.getResource()]: Resource /websocket/t.ws=null 2019-07-18 11:42:30 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.servlet.DefaultServlet.doGet()]: uri=/websocket/t.ws resource=null 2019-07-18 11:42:30 [DEBUG]-[Thread: qtp1526298704-43 - /websocket/t.ws]-[org.eclipse.jetty.server.Server.handle()]: RESPONSE /websocket/t.ws 404 handled=true
请问到底是哪里问题呢?困扰了两天了,jetty升到2019.3启动报错:
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/jetty/server/SessionManager at com.jfinal.core.JFinal.start(JFinal.java:170) at com.common.config.MainConfig.main(MainConfig.java:152) 引入的maven包:
求大佬帮忙看看呢
项目:JFinal
https://www.jfinal.com/doc/1-4