ebSocket.之.基礎入門-斷開連接處理
在《WebSocket.之.基礎入門-后端響應消息》的代碼基礎之上,繼續更新代碼。代碼只改動了:TestSocket.java 和 index.jsp 兩個文件。
先說問題:
當前后端建立連接之后,如果此時關閉瀏覽器,或者點擊瀏覽器的回退。只要退出了建立連接的頁面。后台程序是會報錯的。分別如下圖所示:
正常建立連接頁面:
現在退出當前建立連接的頁面,后台日志如下所示:
1 當前session的id是:0 2 從前端頁面傳過來的數據是:早上好.. 3 十月 14, 2018 8:52:41 上午 org.apache.tomcat.websocket.pojo.PojoEndpointBase onError 4 嚴重: No error handling configured for [com.charles.socket.TestSocket] and the following error occurred 5 java.io.IOException: java.util.concurrent.ExecutionException: java.net.SocketException: Software caused connection abort: socket write error 6 at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:286) 7 at org.apache.tomcat.websocket.WsSession.sendCloseMessage(WsSession.java:572) 8 at org.apache.tomcat.websocket.WsSession.onClose(WsSession.java:495) 9 at org.apache.tomcat.websocket.WsFrameBase.processDataControl(WsFrameBase.java:348) 10 at org.apache.tomcat.websocket.WsFrameBase.processData(WsFrameBase.java:290) 11 at org.apache.tomcat.websocket.WsFrameBase.processInputBuffer(WsFrameBase.java:131) 12 at org.apache.tomcat.websocket.server.WsFrameServer.onDataAvailable(WsFrameServer.java:67) 13 at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler$WsReadListener.onDataAvailable(WsHttpUpgradeHandler.java:204) 14 at org.apache.coyote.http11.upgrade.AbstractServletInputStream.onDataAvailable(AbstractServletInputStream.java:203) 15 at org.apache.coyote.http11.upgrade.AbstractProcessor.upgradeDispatch(AbstractProcessor.java:93) 16 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:635) 17 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) 18 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 19 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 20 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 21 at java.lang.Thread.run(Thread.java:748) 22 Caused by: java.util.concurrent.ExecutionException: java.net.SocketException: Software caused connection abort: socket write error 23 at org.apache.tomcat.websocket.FutureToSendHandler.get(FutureToSendHandler.java:120) 24 at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:281) 25 ... 15 more 26 Caused by: java.net.SocketException: Software caused connection abort: socket write error 27 at java.net.SocketOutputStream.socketWrite0(Native Method) 28 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111) 29 at java.net.SocketOutputStream.write(SocketOutputStream.java:155) 30 at org.apache.coyote.http11.upgrade.BioServletOutputStream.doWrite(BioServletOutputStream.java:38) 31 at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.writeInternal(AbstractServletOutputStream.java:153) 32 at org.apache.coyote.http11.upgrade.AbstractServletOutputStream.write(AbstractServletOutputStream.java:121) 33 at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.onWritePossible(WsRemoteEndpointImplServer.java:94) 34 at org.apache.tomcat.websocket.server.WsRemoteEndpointImplServer.doWrite(WsRemoteEndpointImplServer.java:81) 35 at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.writeMessagePart(WsRemoteEndpointImplBase.java:456) 36 at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessage(WsRemoteEndpointImplBase.java:344) 37 at org.apache.tomcat.websocket.WsRemoteEndpointImplBase.startMessageBlock(WsRemoteEndpointImplBase.java:276) 38 ... 15 more
現在開始處理上面的異常
TestSocket.java
1 package com.charles.socket; 2 3 import java.io.IOException; 4 5 import javax.websocket.OnClose; 6 import javax.websocket.OnMessage; 7 import javax.websocket.OnOpen; 8 import javax.websocket.Session; 9 import javax.websocket.server.ServerEndpoint; 10 11 @ServerEndpoint(value = "/helloSocket") 12 public class TestSocket { 13 14 /*** 15 * 當建立鏈接時,調用的方法. 16 * @param session 17 */ 18 @OnOpen 19 public void open(Session session) { 20 21 System.out.println("開始建立了鏈接..."); 22 System.out.println("當前session的id是:" + session.getId()); 23 } 24 25 /*** 26 * 處理消息的方法. 27 * @param session 28 */ 29 @OnMessage 30 public void message(Session session, String data) { 31 32 System.out.println("開始處理消息..."); 33 System.out.println("當前session的id是:" + session.getId()); 34 System.out.println("從前端頁面傳過來的數據是:" + data); 35 36 37 String message = "你好,我是后端程序..."; 38 try { 39 session.getBasicRemote().sendText(message); 40 } catch (IOException e) { 41 e.printStackTrace(); 42 } 43 44 } 45 46 /*** 47 * 處理斷開連接的方法. 48 * @param session 49 */ 50 @OnClose 51 public void close(Session session) { 52 System.out.println("Session-ID是:"+session.getId() + ",退出了系統...歡迎下次再來..."); 53 } 54 55 }
index.jsp 代碼
1 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 6 <title>Charles-WebSocket</title> 7 8 <script type="text/javascript"> 9 10 var websocket = null; 11 var target = "ws://localhost:8080/websocket/helloSocket"; 12 13 function buildConnection() { 14 15 if('WebSocket' in window) { 16 websocket = new WebSocket(target); 17 } else if('MozWebSocket' in window) { 18 websocket = MozWebSocket(target); 19 } else { 20 window.alert("瀏覽器不支持WebSocket"); 21 } 22 23 // 添加監聽消息的方法 24 websocket.onmessage = function(event) { 25 console.log(event) 26 console.log(event.data) 27 document.getElementById("serverMsg").innerHTML = "<p>后端消息 :"+ event.data +"</p>" 28 } 29 30 // 監聽斷開連接的方法. 31 websocket.onclose = function(event) { 32 // 如沒有業務需求,可以不寫這個關閉監聽的方法. 33 // 業務需求,例如:聊天室,當某人退出的時候,會給出提示,xxx退出了... 34 } 35 } 36 37 // 退出系統時, 關閉建立的WebSocket鏈接 38 window.onbeforeunload = function () { 39 websocket.close(); 40 } 41 42 43 // 往后台服務器發送消息. 44 function sendMessage() { 45 46 var sendmsg = document.getElementById("sendMsg").value; 47 console.log("發送的消息:" + sendmsg); 48 49 // 發送至后台服務器中. 50 websocket.send(sendmsg); 51 } 52 53 </script> 54 </head> 55 <body> 56 57 <button onclick="buildConnection();">開始建立鏈接</button> 58 <hr> 59 <input id="sendMsg" /> <button onclick="sendMessage();">消息發送</button> 60 <div id="serverMsg"></div> 61 62 </body> 63 </html>
訪問瀏覽器,輸入地址:http://localhost:8080/websocket
建立連接,發送消息,然后在退出,在發送消息,在退出
在次訪問系統..並退出...
系統正常運行,異常信息(... Caused by: java.net.SocketException: Software caused connection abort: socket write error.. )解決.
如有問題,歡迎糾正!!!
如有轉載,請標明源處:https://www.cnblogs.com/Charles-Yuan/p/9785147.html