# ws client
swoole_http_client调用upgrade,发起WebSocket握手请求,并将连接升级为WebSocket。
~~~
function swoole_http_client->upgrade(string $path, callable $callback);
~~~
* $path URL路径
* $callback 握手成功或失败后回调此函数
* 使用Upgrade方法必须设置onMessage回调函数
**使用实例**
~~~
$cli = new swoole_http_client('127.0.0.1', 9501);
$cli->on('message', function ($_cli, $frame) {
var_dump($frame);
});
$cli->upgrade('/', function ($cli) {
echo $cli->body;
$cli->push("hello world");
});
~~~
**onMessage回调**
~~~
function onMessage(swoole_http_client $client, swoole_websocket_frame $frame);
~~~
* $client 客户端对象,可调用push方法向服务器发送数据
* $frame WebSocket数据帧,可参考 swoole_websocket_server->onMessage
**异步websocket客户端**
```php
class Ws
{
public $parse_host;
public $parse_port;
public $client;
public $onMessage = null;
public $onConnect = null;
public $onClose = null;
public $upgrade = null;
public function __construct($host)
{
$this->onMessage = function(){};
$this->upgrade = function(){};
$this->onClose = function(){};
$this->onConnect = function(){};
$this->parse_url_to_array($host);
}
public function parse_url_to_array($url)
{
$parsed_arr = parse_url($url);
$this->parse_host = isset($parsed_arr['host']) ? $parsed_arr['host'] : '127.0.0.1';
$this->parse_port = isset($parsed_arr['port']) ? $parsed_arr['port'] : '80';
}
public function connect()
{
$this->client = $client = new \Swoole\Http\Client($this->parse_host, $this->parse_port);
//设置事件回调函数
$this->client->on("message", $this->onMessage);
$this->client->on("connect", $this->onConnect);
$this->client->on("close", $this->onClose);
$this->client->upgrade('/', $this->upgrade);
}
}
$url = 'laychat.workerman.net:9292';
$tcp = new Ws($url);
$tcp->onConnect = function (Client $client) {
var_dump($client);
};
$tcp->onMessage = function (Client $client,$data) {
$client->push('{"type":"ping"}');
var_dump($data);
};
$tcp->connect();
- swoole简介
- swoole功能概述
- 序章
- 开发必读
- 1 环境搭建
- 1.1 环境搭建
- 1.2 搭建Echo服务器
- 2 初识Swoole
- 2.1 Worker进程
- 2.2 TaskWorker进程
- 2.3 Timer定时器
- 2.4 Process进程
- 2.5 Table内存表
- 2.6 多端口监听
- 2.7 sendfile文件支持
- 2.8 SSL支持
- 2.9 热重启
- 2.10 http_server
- 附录*server配置
- 附录*server函数
- 附录*server属性
- 附录*server回调函数
- 附录*server高级特性
- 心跳检测
- 3 Swoole协议
- 3.1 EOF协议
- 3.2 固定包头协议
- 3.3 Http协议
- 3.4 WebSocket协议
- 3.5 MTQQ协议
- 内置http_server
- 内置websocket_server
- Swoole\Redis\Server
- 4 Swoole异步IO
- 4.1 AsyncIO
- 异步文件系统IO
- swoole_async_readfile
- swoole_async_writefile
- swoole_async_read
- swoole_async_write
- 5 swoole异步客户端
- ws_client
- http_client
- mysql_client
- redis_client
- tcp_client
- http2_client
- 6 swoole协程
- Swoole\Coroutine\Http\Client
- Swoole\Coroutine\MySQL
- Swoole\Coroutine\Redis
- Coroutine\PostgreSQL
- Swoole\Coroutine\Client
- Swoole\Coroutine\Socket
- Swoole\Coroutine\Channel
- Coroutine
- Swoole\Coroutine::create
- Swoole\Coroutine::resume
- Swoole\Coroutine::suspend
- Swoole\Coroutine::sleep
- Coroutine::getaddrinfo
- Coroutine::gethostbyname
- swoole_async_dns_lookup_coro
- Swoole\Coroutine::getuid
- getDefer
- setDefer
- recv
- Coroutine::stats
- Coroutine::fread
- Coroutine::fget
- Coroutine::fwrite
- Coroutine::readFIle
- Coroutine::writeFIle
- Coroutine::exec
- 7 swoole_process
- process::construct
- process::start
- process::name
- process::signal
- process::setaffinity
- process::exit
- process::kill
- process::daemon
- process->exec
- process::wait
- process::alarm
- 8 swoole定时器
- swoole_timer_tick
- swoole_timer_after
- swoole_timer_clear
- 9 swoole_event
- swoole_event_add
- swoole_event_set
- swoole_event_del
- swoole_event_wait
- swoole_event_defer
- swoole_event_write
- swoole_event_exit
- swoole提供的function
- 常见问题
- 客户端链接失败原因
- 如何设置进程数
- 如何实现异步任务
- 如何选择swoole三种模式
- php中哪些函数是阻塞的
- 是否可以共用1个redis或mysql连接
- 如何在回调函数中访问外部的变量
- 为什么不要send完后立即close
- 不同的Server程序实例间如何通信
- MySQL的连接池、异步、断线重连
- 在php-fpm或apache中使用swoole
- 学习Swoole需要掌握哪些基础知识
- 在phpinfo中有在php-m中没有
- 同步阻塞与异步非阻塞选择
- CURL发送POST请求服务器端超时
- 附录
- 预定义常量
- 内核参数调优
- php四种回调写法
- 守护进程程序常用数据结构
- swoole生命周期
- swoole_server中内存管理机制
- 使用jemalloc优化swoole内存分配性能
- Reactor、Worker、Task的关系
- Manager进程
- Swoole的实现
- Reactor线程
- 安装扩展
- swoole-worker手册
- swoole相关开源项目
- 写在后面的话
- 版本更新记录
- 4.0