Home  >  Article  >  PHP Framework  >  workerman 403 error causes and solutions

workerman 403 error causes and solutions

尚
Original
2019-12-05 10:52:313247browse

workerman 403 error causes and solutions

workerman encountered the following error:

400 Bad Request
Sec-WebSocket-Key not found.
This is a WebSocket service and can not be accessed via HTTP.

Error reason

This error indicates that you used the http protocol to access the websocket protocol service . Recommendation: workermanTutorial

Developers should note that the application layer protocol used by the client must be the same as the application layer protocol of the server. That is, whatever protocol the server uses, the client uses it. protocol.

If the protocol does not correspond, there will be situations like this refusal of communication or even errors.

This principle is just like accessing the ip:3306 port of the database in the browser address bar. You don't expect the database to really return any useful information to you, right?

Correct approach

The correct approach should be to establish a link to the websocket protocol and use the websocket protocol to communicate with the workererman's websocket protocol service. If the client is a browser, you can use js to establish a websocket link. The code is similar to this:

// 假设服务端ip为127.0.0.1,端口为2346
ws = new WebSocket("ws://127.0.0.1:2346");
ws.onopen = function() {
    alert("连接成功");
    ws.send('tom');
    alert("给服务端发送一个字符串:tom");
};
ws.onmessage = function(e) {
    alert("收到服务端的消息:" + e.data);
};

The above is the detailed content of workerman 403 error causes and solutions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn