Wrench

Simple WebSocket Client/Server for PHP

Documentation: wrench.readthedocs.org

A simple websocket server and client package for PHP 7.1.

Installation

The library is PSR-4 compatible, with a vendor name of Wrench. It's available on Composer as wrench/wrench, so you can:

composer require wrench/wrench ~3.0

Usage

This creates a server on 127.0.0.1:8000 with one Application that listens for WebSocket requests to ws://localhost:8000/echo and ws://localhost:8000/chat:

Server

/** * An example application, that just echoes the received data back to the connection that sent it */ $app = new class implements WrenchApplicationDataHandlerInterface { public function onData(string $data, WrenchConnection $connection): void { $connection->send($data); } }; // A websocket server, listening on port 8000 $server = new WrenchBasicServer('ws://localhost:8000', array( 'allowed_origins' => array( 'mysite.com', 'mysite.dev.localdomain' ) )); $server->registerApplication('echo', $app); $server->registerApplication('chat', new MyChatApplication()); $server->setLogger($monolog); // PSR3 $server->run();

Client

/** * A client side example, that sends a string and will receive the data back to the connection that sent it */ $client = new Client('ws://localhost:8000, http://localhost:8000); $client->connect(); $client->sendData('hello'); $response = $client->receive()[0]->getPayload(); $client->disconnect();

Releases and Changelog

See the CHANGELOG for detailed information about changes between releases.

PHP5 Support

The latest major release dropped support for PHP versions prior to 7.1. If you need support for older versions of PHP, see the 2.0 branch. The latest 2.0 branch release is 2.0.8. You can install it with:

composer require wrench/wrench ~2.0

Developing

Run ./vendor/bin/phpcs and fix any errors that you come across Run ./vendor/bin/phpunit --exclude-group large,medium for a fast test between changes

See Also

Ratchet an excellent Websocket layer for React.

版权声明:

1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。
2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。