lowerpower / bolt
基于 ReactPHP 构建的异步 WebSocket 客户端
0.2.0
2017-10-19 22:08 UTC
Requires
- clue/block-react: ^1.2
- react/child-process: ^0.5.0
- react/datagram: ^1.1
- react/dns: ^0.4.11
- react/event-loop: ^0.4
- react/http: ^0.7.2
- react/http-client: ^0.5
- react/promise: ^2.5
- react/promise-timer: ^1.2
- react/socket: ^0.8
This package is not auto-updated.
Last update: 2024-09-18 04:20:12 UTC
README
PHP 的异步 WebSocket 客户端库。支持 HyBi,以及 Hixie #76(无意义)。
这个库纯粹是为了作为客户端而构建的,因为大部分可用的 WS 客户端也包含了服务器,而且体积非常大。
这不是原始版本,已经更新以使用最新的 reactphp,API 也已经被修改以更符合现代的 reactphp。
安装
使用 composer
"require": {
"lowerpower/bolt": "^0.2"
}
使用方法
由于这个库非常适合构建异步应用程序,它被构建为使用 React 事件循环和解析器,因为它们很可能已经在你的项目中,这样你就可以连接到同一个循环。
$loop = \React\EventLoop\Factory::create(); // other options like tls, socket, etc are possible see "React\Socket\Connector" $options=$options=array( 'timeout'=>5.0 /* 'timeout'=> false for disable */ ); // Now workes exaction like "React\Socket\Connector" $client = new \Calcinai\Bolt\Client($loop, $options); //Most WS servers will complain/forbid if there is no origin header $client->setOrigin('127.0.0.1'); // new, URL is here, ws:// and wss:// supported $client->connect('ws://127.0.0.1:1337/chat'); $client->on('stateChange', function($newState){ echo "State changed to: $newState\n"; }); $client->on('message', function($message) use ($client){ echo "New message: \n"; echo $message; $client->send('This is a response message'); }); $loop->run();
其他可用的事件包括 connecting
、connect
、closing
、close
也支持通过 URI 的 HTTP 基本认证,形式为 user:pass@host
任何反馈都欢迎