swoole 如何实现 websocket 客户端的功能?

workerman 的 websocket 客服端是这样实现的:(很简单,很容易就使用了,和前端差不多)

http://doc.workerman.net/315306

swoole 如何实现这样相同的功能呢?

看了一下的一些文档,始终无法理解:

https://github.com/matyhtf/framework/blob/master/libs/Swoole/Client/WebSocket.php

https://wiki.swoole.com/wiki/page/p-http_client.html

如果只是获取的话,比较简单:

<?php
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);

$client->on('connect', function($cli) {
    $cli->send("GET / HTTP/1.1\r\n\r\n");
});
$client->on('receive', function($cli, $data) {
    echo "Received: " . $data;
});
$client->on('error', function($cli) {
    echo "Connect failed\r\n\r\n";
});
$client->on('close', function($cli) {
    echo "Connection close\r\n\r\n";
});

$client->connect('xxx.xxx.xxx.xxx', 1234, true);

但是我这里想要实现的是类似 workerman 那样的,可以一直连接。

作者: 执着小钟

执着小钟

发表评论