当前位置: 首页 > 工具软件 > binance > 使用案例 >

TP+WorkerMan访问binance wss端口

卫彭亮
2023-12-01
define('BIND_MODULE','api/Worker/start');服务启动文件,启动start方法
    public function start()
    {
        $this->getMarkPrice();
        Worker::runAll();
    }

getMarkPrice()方法内访问外部wss端口websocket服务

方法内new Worker(),设置worker子进程启动时的回调函数onWorkerStart

new AsyncTcpConnection('');创建一个异步链接对象,作为客户端向服务端发送通过send和onMessage异步发送和处理链接上的数据。
            $binance = new AsyncTcpConnection('ws://fstream.binance.com/ws/bnbusdt@markPrice');
            $binance->transport = 'ssl';
            $binance->onConnect =function ($binance)
            {
                echo "success";
            };

            $binance->onMessage = function (AsyncTcpConnection $binance, $data) {
                var_dump($data);
            };

            $binance->connect();

返回结果

string(147) "{"e":"markPriceUpdate","E":1642988358009,"s":"BNBUSDT","p":"374.60277560","P":"376.98405577","i":"374.90433294","r":"0.00000000","T":1643011200000}"
 类似资料: