按照网上的一篇教程来的。
控制器:
package com.jfinal.common.websocket; import java.io.IOException; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.atomic.AtomicInteger; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.ServerEndpoint; import com.jfinal.log.Log; @ServerEndpoint("/websocket") public class WebSocketController { static Log log = Log.getLog(WebSocketController.class); private static final String GUEST_PREFIX = "Guest"; private static final AtomicInteger connectionIds = new AtomicInteger(0); private static final Set<WebSocketController> connections = new CopyOnWriteArraySet<>(); @OnOpen public void onOpen(Session session) { } @OnClose public void onClose(Session session) { } @OnMessage public void onMessage(String requestJson, Session session) throws IOException { session.getBasicRemote().sendText(requestJson); } }
Handler
package com.jfinal.common.websocket; 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); } }
配置
@Override public void configHandler(Handlers me) { me.add(new ContextPathHandler("ctx")); me.add(new WebSocketHandler("^/websocket")); }
HTML
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <!-- for HTML5 --> <title>Java后端WebSocket的Tomcat实现</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:81/websocket"); } 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>
结果运行的时候是404,找了一天问题了。烦请赐教。
项目:JFinal
终极解决方法是设置断点,单步调试到 WebSocketHandler 中去看程序走向,不要去猜测程序的行为,要调试