Tag Archives: websocket

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.

Solve the 400 error of nginx forwarding websocket

Original address: https://www.cnblogs.com/duanweishi/p/9286461.html

explain

Because there are multiple items on the personal server and the secondary domain name is configured, the secondary domain name needs to be forwarded. In the forwarding work, the famous nginx is adopted. Before that, there was no problem in forwarding all projects. However, when deploying a project with websocket communication, an unexpected error was reported. The error message is as follows:

1failed: Error during WebSocket handshake: Unexpected response code: 400

。 There is no problem with this error in the local test environment and accessing non nginx forwarding. Therefore, it is inferred that the problem should occur in nginx forwarding.

So, with the help of Google , I saw socket. IO   The official issues has a discussion on this issue, link: https://github.com/socketio/socket.io/issues/1942

Solution

After reading the scheme in the discussion area, the problem appears in the configuration file of nginx , and the nginx.conf file needs to be modified. In the linux terminal, type VIM/etc/nginx/nginx.conf , find the location , and the configuration file is as follows:

1server {
2        listen       80;
3        server_name  school.godotdotdot.com;
4        charset utf-8;
5
6        location/{
7            proxy_pass http://127.0.0.1:3000;
8            proxy_set_header Host $host;
9            proxy_http_version 1.1;
10            proxy_set_header Upgrade $http_upgrade;
11            proxy_set_header Connection "upgrade";
12            proxy_set_header X-Real-IP $remote_addr;
13            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
14            proxy_connect_timeout 60;
15            proxy_read_timeout 600;
16            proxy_send_timeout 600;
17        }
18
19        error_page   500 502 503 504  /50x.html;
20        location = /50x.html {
21            root   html;
22        }
23
24    }

The most important of these are the following three lines

1proxy_http_version 1.1;
2proxy_set_header Upgrade $http_upgrade;
3proxy_set_header Connection "upgrade";

The first line tells nginx to use the http/1.1 communication protocol, which must be used by websoket .

The second and third lines tell nginx to respond to the HTTP upgrade request when it wants to use websocket.