<dependency> <groupId>com.jfinal</groupId> <artifactId>jfinal</artifactId> <version>3.4</version> </dependency> <dependency> <groupId>javax.websocket</groupId> <artifactId>javax.websocket-api</artifactId> <version>1.1</version> <scope>provided</scope> </dependency> /** * 配置全局处理器 */ @Override public void configHandler(Handlers handler) { // log.info("configHandler 全局配置处理器,设置跳过哪些URL不处理"); handler.add(new UrlSkipHandler( "/|/ca/.*|/se/.*|.*.htm|.*.html|.*.js|.*.css|.*.json|.*.png|.*.gif|.*.jpg|.*.jpeg|.*.bmp|.*.ico|.*.exe|.*.txt|.*.zip|.*.rar|.*.7z|.*.tff|.*.woff|.*.ttf|.*.map|.*.xml|.*.woff2", false)); handler.add(new GlobalHandler()); handler.add(new WebSocketHandler("^/websocket")); }
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); } }
@ServerEndpoint("/websocket") public class WebSocketController { private static CopyOnWriteArraySet<WebSocketController> webSocketSet = new CopyOnWriteArraySet<WebSocketController>(); private Session session; public WebSocketController() { System.out.println(" WebSocket init~~~"); } @OnOpen public void onOpen(Session session) { System.out.println("onOpen"); this.session = session; webSocketSet.add(this); } @OnClose public void onClose(Session session) { System.out.println("onClose"); this.session = session; webSocketSet.remove(this); } // @OnError // public void onError(Session session) { // System.out.println("onError"); // } @OnMessage public void onMessage(String requestJson, Session session) throws IOException { requestJson = "后台回复:" + requestJson; System.out.println(requestJson); session.getBasicRemote().sendText(requestJson); } }
前端页面
var websocket = null; //判断当前浏览器是否支持WebSocket if ('WebSocket' in window) { websocket = new WebSocket("ws://localhost:8080"+ctx+"/websocket"); // websocket = new WebSocket("ws://localhost:8080/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) { // alert(innerHTML); } //关闭WebSocket连接 function closeWebSocket() { websocket.close(); } //发送消息 function send() { var message = "发送销项"; websocket.send(message); }
页面加载时后台报错:
WebSocket init~~~ onOpen onClose 七月 02, 2020 9:02:55 上午 org.apache.tomcat.websocket.pojo.PojoEndpointBase onError 严重: No error handling configured for [com.mnt.websocket.WebSocketController] and the following error occurred java.io.IOException: java.util.concurrent.ExecutionException: java.net.SocketException: Software caused connection abort: socket write error at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:286) at org.apache.tomcat.websocket.WsSession.sendCloseMessage(WsSession.java:572) at org.apache.tomcat.websocket.WsSession.doClose(WsSession.java:466) at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.onError(WsHttpUpgradeHandler.java:161) at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.access$300(WsHttpUpgradeHandler.java:47) at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onError(WsHttpUpgradeHandler.java:225) at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:208) at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:203) at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:93) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:635) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source) Caused by: java.util.concurrent.ExecutionException: java.net.SocketException: Software caused connection abort: socket write error at org.apache.tomcat.websocket.FutureToSendHandler.get(FutureToSendHandler.java:120) at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:281) ... 14 more Caused by: java.net.SocketException: Software caused connection abort: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at org.apache.coyote.http11.upgrade.BioServletOutputStream.doWrite(BioServletOutputStream.java:38) at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.writeInternal(AbstractServletOutputStream.java:153) at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.write(AbstractServletOutputStream.java:121) at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.onWritePossible(WsRemoteEndpointImplServer.java:94) at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.doWrite(WsRemoteEndpointImplServer.java:81) at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.writeMessagePart(WsRemoteEndpointImplBase.java:456) at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessage(WsRemoteEndpointImplBase.java:344) at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:276) ... 14 more 七月 02, 2020 9:02:55 上午 org.apache.tomcat.websocket.pojo.PojoEndpointBase onError 严重: No error handling configured for [com.mnt.websocket.WebSocketController] and the following error occurred java.io.EOFException at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:64) at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:204) at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:203) at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:93) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:635) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)
跪求报错原因。。。。
项目:JFinal
try {
session.getBasicRemote().sendText(requestJson);
} catch (IOException e) {
LogKit.error("客户端异常", e);
}
错误原因,控制台已经说了 Software caused connection abort: socket write error
或者再加一个方法@OnError