'servers' => [
...http
// websocket
'name' => 'ws',
'type' => Server::SERVER_WEBSOCKET,
'host' => '0.0.0.0',
'port' => 9502,
'sock_type' => SWOOLE_SOCK_TCP,
'callbacks' => [
SwooleEvent::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
SwooleEvent::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
SwooleEvent::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
// routes.php
Router::addServer('ws', function () {
Router::get('/ws', 'App\Controller\Chat\WebSocketControllerSelf');
// WebSocketControllerSelf.php
declare(strict_types=1);
namespace App\Controller\Chat;
use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Swoole\Http\Request;
use Swoole\Server;
use Swoole\Websocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer;
class WebSocketControllerSelf implements OnMessageInterface, OnOpenInterface, OnCloseInterface
public function onMessage(WebSocketServer $server, Frame $frame): void
$server->push($frame->fd, 'Recv: ' . $frame->data);
public function onClose(Server $server, int $fd, int $reactorId): void
var_dump('closed');
public function onOpen(WebSocketServer $server, Request $request): void
$server->push($request->fd, 'Opened');
nginx的配置如下
upstream hyperf_websocket {
# 设置负载均衡模式为 IP Hash 算法模式,这样不同的客户端每次请求都会与同一节点进行交互
ip_hash;
# Hyperf WebSocket Server 的 IP 及 端口
server 127.0.0.1:9502;
server {
listen 80;
location / {
proxy_pass http://localhost:9501;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location /ws {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 将客户端的 Host 和 IP 信息一并转发到对应节点
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
# 客户端与服务端无交互 60s 后自动断开连接,请根据实际业务场景设置
proxy_read_timeout 60s ;
# 执行代理访问真实服务器
proxy_pass http://hyperf_websocket;
前端页面访问 http://host/ws ,js socket报错
app.js:19 Uncaught DOMException: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
at http://192.168.33.10/chat/js/app.js:19:4
(anonymous) @ app.js:19
app.js:1 WebSocket connection to 'ws://192.168.33.10/ws' failed: Error during WebSocket handshake: 'Upgrade' header value is not 'WebSocket': WebSocketController
(anonymous) @ app.js:1
app.js:16 WebSocket异常!
// ------------------------------------
// js代码
var ws = new WebSocket("ws://192.168.33.10/ws");//连接服务器
ws.onopen = function (event) {
console.log(event);
ws.onmessage = function (event) {
console.log('onmessage')
ws.onclose = function (event) {
alert("已经与服务器断开连接\r\n当前连接状态:" + this.readyState);
ws.onerror = function (event) {
console.log("WebSocket异常!");
ws.send('hello hyperf socket');
这个代码是我做初步测试的socket的,我现在是想调通连接socket服务,暂未完善代码。
即使我删掉了 ws.send('hello hyperf socket'); 这一行,也是报一样的报错。
只是chrome浏览器控制台少了一行报错。
------------------ 原始邮件 ------------------
发件人: JiangHuang <
[email protected]>
发送时间: 2019年10月28日 21:22
收件人: hyperf/hyperf <
[email protected]>
抄送: 李铭昕 <
[email protected]>, Assign <
[email protected]>
主题: 回复:[hyperf/hyperf] 原生js无法连接socket服务端 (
#801)
// routes.php
Router::addServer('ws', function () {
Router::get('/', 'App\Controller\Chat\WebSocketControllerSelf');
路由改成这样试试,感觉是你 nginx 代理的时候的问题
我重新安装了框架,:( 代码貌似和以前一样, socket服务连接正常了。
只不过我在composer安装的时候不选择安装额外的包,上次安装的时候任何包都安装了。不确定是不是这个原因。
谢谢,谢谢~~