zoukankan      html  css  js  c++  java
  • WebSocket.之.基础入门-断开连接处理

    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

  • 相关阅读:
    离散数学概论
    Linux内核分析
    程序的本质
    常见bug分析
    java编程思想--学习心得
    领域特定语言--心得
    Linux下网卡配置
    ubuntu下安装python的gevent模块遇到的一个问题
    二分图的最小点覆盖和最大独立集
    win7通过ssh远程登录mininet虚拟机,运行wireshark并通过x11在宿主机显示图形界面
  • 原文地址:https://www.cnblogs.com/Charles-Yuan/p/9785147.html
Copyright © 2011-2022 走看看