Tag Archives: Unable to unwrap data

WebSocket Error: Unable to unwrap data, invalid status [CLOSED]

1. Origin of the problem

In the current project, the technology of websocket is mainly used for real-time data transmission at the server and client. Because timely response is required, HTTP request is not used,

Instead, socket is used, which can quickly establish a connection and respond to the operation of the applet in the client unit program in real time. When I first started this project, I was right

For technology selection, we consider using websocket. We often see its introduction and use in various blogs and forums. And it doesn’t feel too difficult, so I plan to use it. client

The program is mainly a unity program for processing. It will communicate with the server written in Java in real time and transmit a lot of data. At the beginning of the test, we don’t know whether this scheme can work. After many times

After repeated testing, it is found that this method is feasible, and finally this technology is applied to the current system. However, there was a problem in the process of using it later, that is, when the websocket runs abnormally,

For example, after the connection between the client and the server is disconnected due to the bad network, errors will be reported frequently when the network returns to normal and connects again,

The error message is the heartbeat packet data sent by the client. Because the client does not know that the server has a problem, it always reports an error when sending heartbeat packets frequently.

2. Problem analysis

When you write to the server, when you run the onerror method, you will clear a unique websocket connection. Due to the particularity of this project, you only need to keep an effective connection between the server and the client

Just. My idea is that I clearly know the only connection on the server. Why does this problem still occur?

This problem will appear every once in a while and must be solved. If it is not solved, it will certainly affect the normal operation of the project.

3. Solution

After careful analysis of the problem, I tried to solve the problem one by one.

Scheme 1: when there is an error in operation, actively call the method of closing the connection provided in websocket and use this to call.

Test results, not resolved.

Scheme 2: in case of running error, actively obtain the unique websockerserver object in the collection, and then use this object to call onclose method and close the current connection

Session.

After the code is modified, conduct repeated joint debugging and testing with the client to find and solve the problem. The idea to solve this problem is to take the initiative on the server if the server runs abnormally

Close this connection; After the connection is closed, when the client and server want to communicate again, a new connection will be created to ensure the normal operation of the system.

So far, the problem has been solved, which may be due to the particularity of the project. The project only needs a unique connection, and the problems encountered are easy to solve. To sum up, if

If the server wants to close a connection, it’s best to find the websocket connection first and then close it. In addition, close the current session information. You can’t use the collection to clear it directly,

In combination with direct clearing, the websocketserver is cleared, but the connection session information is still there, so I have that problem.