Don’t panic when you encounter this problem, go check the opinions. Let me summarize here
There are several situations where this problem exists
1. The ws connection is unstable and often disconnected;
answer:
1) It may be a network problem between the client and the server
2) The thread may be cleaned up abnormally due to insufficient memory on the server side
2. After the ws connection is successful, it will automatically disconnect after a while
answer:
1) In general, nginx forwarding or tomcat connection timeout causes the container layer to actively close the connection
(1) Modify the container layer tomcat or nginx [proxy_read_timeout 5000s ; keepalive_timeout 5000s ; ] configuration is enough , but it is not recommended to treat the symptoms rather than the root cause
(2) The server actively sends a heartbeat message to the client. It is not recommended because it will cause the server to load
(3) The client takes the initiative to send a heartbeat message to the server. I personally recommend this solution
3. After the ws connection, the server actively closes the connection due to various reasons such as authentication failure, which causes the exception to be thrown
Answer: The solution code is below [The key point is the CloseReason.CloseCodes.TLS_HANDSHAKE_FAILURE error code. When you use this error code, it will cause an io error, which means handshake failure]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/** * Operation for downline * @param session */ public void optClose(Session session){ // Determine if the current connection is still online if (session.isOpen()){ try { // Closed CloseReason closeReason = new CloseReason(CloseReason.CloseCodes.NORMAL_CLOSURE, "Error!" ); session.close(closeReason); } catch (IOException e) { e.printStackTrace(); } } } |
Note: When an abnormal error occurs, the connection should be closed in time and the related users’ online and offline operations should be handled!
Read More:
- Java learning unreported exception java.io.IOException ; must be caught or declared to be thrown
- [Solved] ClientAbortException: java.io.IOException: Connection reset by peer
- Springboot integrates Redis annotation and access error: java.io.NotSerializableException: com.demo.entity.MemberEntity
- [Solved] Multithreading uses jsch to obtain a session for connection error: session.connect: java.net.socketexception: connection reset
- [Solved] IDEA springboot Startup Error: java.lang.UnsatisfiedLinkError: no tcnative-1 in java.library.path
- How to Solve java server error (java application Run Normally)
- [Solved] Springboot Project Error: Mail server connection failed;
- [Solved] Java.lang.ClassCastException: [Ljava.lang.Long; cannot be cast to java.util.List
- [How to Solve Error]java.util.Date cannot be cast to java.sql.Date
- How to Solve Error:java.io.InvalidClassException
- [Solved] failed on connection exception: java.net.ConnectException: Connection denied
- [Solved] SpringBoot+Dubbo Startup Error: Fail to start server(url: dubbo://192.168.0.9:20880/service
- I/O error while reading input message; nested exception is java.io.IOException: Stream closed
- [Solved] SpringBoot+Dubbo Startup Error: Fail to start server(url dubbo192.168.0.920880service
- [Solved] Read the resources resource and convert it to file error: java.io.filenotfoundexception
- [Solved] Hadoop failed on connection exception: java.net.ConnectException: Connection refused
- [Solved] Springboot uses redis to add LocaldateTime Java 8 error
- Run hadoop fs -put Command Error: java.io.IOException: Got error, status message , ack with firstBadLink
- [Solved] hadoop Error: 9000 failed on connection exception java.net.ConnectException Denied to Access
- [Solved] Java.util.linkedhashmap cannot be cast to entity class