使用Swoole 异步客户端连接域名地址如何设置?

参考https://wiki.swoole.com/wiki/page/p-http_client.html 写了swoole 异步客户端。

因为有需求,所以需要采用异步客户端,实时可以监听推送数据。

但访问这个WebSocket服务连接地址:wss://real.okex.com:10441/websocket 没有反应。

[Java对接文档] https://github.com/okcoin-okex/API-docs-OKEx.com/blob/master/API-For-Spot-CN/%E5%B8%81%E5%B8%81%E4%BA%A4%E6%98%93WebSocket%20API.md

这是PHP实现:

        \Swoole\Async::dnsLookup("wss://real.okex.com", function ($domainName, $ip) {
            $cli = new \Swoole\Http\Client($ip, 10441);
            $cli->setHeaders(array('User-Agent' => 'swoole-http-client',
                'Host' =>  $domainName,
                'Accept' => 'text/html,application/xhtml+xml,application/xml',
                'Accept-Encoding' => 'gzip',
            ));
            $cli->setCookies(array('test' => 'value'));
            $cli->on('message', function ($_cli, $frame) {
                echo '123123';
                echo $frame;
            });

            $cli->upgrade('/websocket', function ($cli) {
                echo $cli->body;
                $cli->push("{'event':'addChannel','channel':'ok_sub_spot_usd_btc_ticker'}");
            });
        });

是哪写错了吗?

2018-05-19 00:30
0
0

韩天峰 - 关注外星人

赞同来自:

你的代码存在问题。正确的写法:

$cli = new \Swoole\Http\Client($domainName, 10441, true);
            $cli->setHeaders(array('User-Agent' => 'swoole-http-client',
                'Host' =>  $domainName,
                'Accept' => 'text/html,application/xhtml+xml,application/xml',
                'Accept-Encoding' => 'gzip',
            ));
            $cli->setCookies(array('test' => 'value'));
            $cli->on('message', function ($_cli, $frame) {
                echo '123123';
                echo $frame;
            });

            $cli->upgrade('/websocket', function ($cli) {
                echo $cli->body;
                $cli->push("{'event':'addChannel','channel':'ok_sub_spot_usd_btc_ticker'}");
            });
2018-05-25 21:51

要回复问题请先登录注册