Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thenlabs/socket-server
An useful library for creating asynchronous network applications with PHP.
https://github.com/thenlabs/socket-server
network network-programming networks php php-library socket socket-io socket-programming sockets socketserver
Last synced: about 1 month ago
JSON representation
An useful library for creating asynchronous network applications with PHP.
- Host: GitHub
- URL: https://github.com/thenlabs/socket-server
- Owner: thenlabs
- License: mit
- Created: 2021-10-11T12:09:33.000Z (about 3 years ago)
- Default Branch: 1.1
- Last Pushed: 2022-04-19T14:54:36.000Z (over 2 years ago)
- Last Synced: 2024-08-09T20:43:12.149Z (3 months ago)
- Topics: network, network-programming, networks, php, php-library, socket, socket-io, socket-programming, sockets, socketserver
- Language: PHP
- Homepage:
- Size: 175 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# SocketServer
An useful library for creating asynchronous network applications with PHP.
>If you like this project gift us a ⭐.
## Features.
- Asynchronous connections.
- Multiple configuration options.
- Event system to implement the application logic.
- Logs support.## Installation.
$ composer require thenlabs/socket-server
## Usage.
The below code show a bare network application that accept multiple connections and forward each incoming message to the rest of the connections.
>That application can be found in the `tests/Functional/hub.php` file. For test it you can run `php tests/Functional/hub.php`.
Can be seen that the `SocketServer` class offers the necessary events for react at the differents connection status.
>Check the `SocketServer` API for knows all his possibilities.
```php
connections as $connection) {
$connection->writeLine("New connection.");
}$this->connections[] = $event->getConnection();
}public function onData(DataEvent $event): void
{
$data = $event->getData();switch ($data) {
case 'exit':
$event->getConnection()->close();
break;case 'stop':
$event->getServer()->stop();
break;default:
foreach ($this->connections as $connection) {
if ($connection != $event->getConnection()) {
$connection->writeLine($data);
}
}
break;
}
}public function onDisconnection(DisconnectionEvent $event): void
{
foreach ($this->connections as $id => $connection) {
if ($connection == $event->getConnection()) {
unset($this->connections[$id]);
break;
}
}
}
}$server = new HubServer(['socket' => $argv[1] ?? 'tcp://127.0.0.1:9000']);
$server->start();
```The above example works as follows:
![](demo.gif)
## Development.
### Running the tests.
For run the tests, runs the next command:
$ ./vendor/bin/pyramidal