添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

环境: vagrant 、ubuntu18.04、swoole4.4、 hyperf1.1、nginx

求教是我哪里配置错了吗?

ws配置config/autoload/server.php与逻辑代码:

'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浏览器控制台少了一行报错。