think-swoole的一些坑

一、php连接websocket服务器内部通信

  1.1、安装ratchet/pawl

  composer require ratchet/pawl

  1.2、代码示例:

\Ratchet\Client\connect('ws://123.57.237.122:9004')->then(function ($conn) {
            $conn->on('message', function ($msg) use ($conn) {
                echo "Received: {$msg}\n";
                $conn->close();
            });
            $arr = [
                'type' => 'submit',
                'name' => '加价',
                'data' => [
                    'goods_id' => 10001,
                    'price' => rand(1, 100)
                ]
            ];
            $conn->send(json_encode($arr, 256));
        }, function ($e) {
            echo "Could not connect: {$e->getMessage()}\n";
        });

 

posted @ 2024-03-01 13:45  转~~~  阅读(92)  评论(0)    收藏  举报