php-websocket

所属分类:Websocket编程
开发工具:PHP
文件大小:0KB
下载次数:0
上传日期:2022-10-22 01:31:37
上 传 者sh-1993
说明:  用PHP实现的简单WebSocket服务器。
(Simple WebSocket server implemented in PHP.)

文件列表:
LICENSE (1070, 2022-08-20)
composer.json (712, 2022-08-20)
examples/ (0, 2022-08-20)
examples/Application/ (0, 2022-08-20)
examples/Application/Chat.php (2619, 2022-08-20)
examples/public/ (0, 2022-08-20)
examples/public/chat.html (523, 2022-08-20)
examples/public/css/ (0, 2022-08-20)
examples/public/css/chat.css (1175, 2022-08-20)
examples/public/css/status.css (1362, 2022-08-20)
examples/public/js/ (0, 2022-08-20)
examples/public/js/chat.js (2633, 2022-08-20)
examples/public/js/status.js (4996, 2022-08-20)
examples/public/status.html (849, 2022-08-20)
examples/push.php (483, 2022-08-20)
examples/server.php (654, 2022-08-20)
src/ (0, 2022-08-20)
src/Application/ (0, 2022-08-20)
src/Application/Application.php (1957, 2022-08-20)
src/Application/ApplicationInterface.php (1171, 2022-08-20)
src/Application/StatusApplication.php (5226, 2022-08-20)
src/Connection.php (17981, 2022-08-20)
src/IPCPayload.php (1015, 2022-08-20)
src/IPCPayloadFactory.php (1492, 2022-08-20)
src/Logger/ (0, 2022-08-20)
src/Logger/StdOutLogger.php (933, 2022-08-20)
src/PushClient.php (2258, 2022-08-20)
src/Server.php (19640, 2022-08-20)
src/Timer.php (800, 2022-08-20)
src/TimerCollection.php (609, 2022-08-20)

Bloatless PHP WebSockets

Simple WebSocket server implemented in PHP.

- [Installation](https://github.com/bloatless/php-websocket/blob/master/#installation) - [Requirements](https://github.com/bloatless/php-websocket/blob/master/#requirements) - [Installation procedure](https://github.com/bloatless/php-websocket/blob/master/#installation-procedure) - [Usage](https://github.com/bloatless/php-websocket/blob/master/#usage) - [Server](https://github.com/bloatless/php-websocket/blob/master/#server) - [Applications](https://github.com/bloatless/php-websocket/blob/master/#applications) - [Timers](https://github.com/bloatless/php-websocket/blob/master/#timers) - [Push-Client (IPC)](https://github.com/bloatless/php-websocket/blob/master/#push-client-ipc) - [Client (Browser/JS)](https://github.com/bloatless/php-websocket/blob/master/#client-browserjs) - [Intended use and limitations](https://github.com/bloatless/php-websocket/blob/master/#intended-use-and-limitations) - [Alternatives](https://github.com/bloatless/php-websocket/blob/master/#alternatives) - [License](https://github.com/bloatless/php-websocket/blob/master/#license) ## Installation ### Requirements * PHP >= 7.4 * ext-json * ext-sockets ### Installation procedure Install the package using composer: `composer require bloatless/php-websocket` ## Usage ### Server After downloading the sourcecode to your machine, you need some code to actually put your websocket server together. Here is a basic exmaple: ```php setMaxClients(100); $server->setCheckOrigin(false); $server->setAllowedOrigin('example.com'); $server->setMaxConnectionsPerIp(20); // add your applications $server->registerApplication('status', \Bloatless\WebSocket\Application\StatusApplication::getInstance()); $server->registerApplication('chat', \Bloatless\WebSocket\Examples\Application\Chat::getInstance()); // start the server $server->run(); ``` Assuming this code is in a file called `server.php` you can then start your server with the following command: ```shell php server.php ``` The websocket server will then listen for new connections on the provided host and port. By default, this will be `localhost:8000`. This repositoy also includes a working example in [examples/server.php](https://github.com/bloatless/php-websocket/blob/master/examples/server.php) ### Applications The websocket server itself handles connections but is pretty useless without any addional logic. This logic is added by applications. In the example above two applications are added to the server: `status` and `chat`. The most important methods in your application will be: ```php interface ApplicationInterface { public function onConnect(Connection $connection): void; public function onDisconnect(Connection $connection): void; public function onData(string $data, Connection $client): void; public function onIPCData(array $data): void; } ``` `onConnect` and `onDisconnect` can be used to keep track of all the clients connected to your application. `onData` will be called whenever the websocket server receives new data from one of the clients connected to the application. `onIPCData` will be called if data is provided by another process on your machine. (See [Push-Client (IPC)](https://github.com/bloatless/php-websocket/blob/master/#push-client-ipc)) A working example of an application can be found in [examples/Application/Chat.php](https://github.com/bloatless/php-websocket/blob/master/examples/Application/Chat.php) ### Timers A common requirement for long-running processes such as a websocket server is to execute tasks periodically. This can be done using timers. Timers can execute methods within your server or application periodically. Here is an example: ```php $server = new \Bloatless\WebSocket\Server('127.0.0.1', 8000, '/tmp/phpwss.sock'); $chat = \Bloatless\WebSocket\Examples\Application\Chat::getInstance(); $server->addTimer(5000, function () use ($chat) { $chat->someMethod(); }); $server->registerApplication('chat', $chat); ``` This example would call the method `someMethod` within your chat application every 5 seconds. ### Push-Client (IPC) It is often required to push data into the websocket-server process from another application. Let's assume you run a website containing a chat and an area containing news or a blog. Now every time a new article is published in your blog you want to notify all users currently in your chat. To achieve this you somehow need to push data from your blog logic into the websocket server. This is where the Push-Client comes into play. When starting the websocket server, it opens a unix-domain-socket and listens for new messages. The Push-Client can then be used to send these messages. Here is an example: ```php $pushClient = new \Bloatless\WebSocket\PushClient('//tmp/phpwss.sock'); $pushClient->sendToApplication('chat', [ 'action' => 'echo', 'data' => 'New blog post was published!', ]); ``` This code pushes data into your running websocket-server process. In this case the `echo` method within the chat-application is called and it sends the provided message to all connected clients. You can find the full working example in: [examples/push.php](https://github.com/bloatless/php-websocket/blob/master/examples/push.php) **Important Hint:** Push messages cannot be larger than 64kb! ### Client (Browser/JS) Everything above this point was related to the server-side of things. But how to connect to the server from your browser? Here is a simple example: ```html ``` This javascript connects to the chat application on your server and prints all incoming messages into the console. A better example of the chat client can be found in: [examples/public/chat.html](https://github.com/bloatless/php-websocket/blob/master/examples/public/chat.html) ## Intended use and limitations This project was mainly built for educational purposes. The code is relatively simple and easy to understand. This server was **not tested in production**, so I strongly recommend not to use it in a live project. It should be totally fine for small educational projects or internal tools, but most probably will not handle huge amounts of traffic or connections very well. Also, some "features" are missing by design: * SSL is not supported. If required, you can use a reverse proxy like nginx. * Binary messages are not supported. * A lot of other stuff I did not even realize ;) In case you need a more "robust" websocket server written in PHP, please have a look at the excellent alternatives listed below. ## Alternatives * [Ratchet](https://github.com/bloatless/php-websocket/blob/master/https://github.com/ratchetphp/Ratchet) * [Wrench](https://github.com/bloatless/php-websocket/blob/master/https://github.com/varspool/Wrench) ## License MIT

近期下载者

相关文件


收藏者