|
|
@@ -19,11 +19,9 @@ composer require workerman/workerman
|
|
|
## Basic Usage
|
|
|
|
|
|
### A websocket server
|
|
|
-test.php
|
|
|
```php
|
|
|
<?php
|
|
|
use Workerman\Worker;
|
|
|
-require_once './Workerman/Autoloader.php';
|
|
|
|
|
|
// Create a Websocket server
|
|
|
$ws_worker = new Worker("websocket://0.0.0.0:2346");
|
|
|
@@ -55,9 +53,8 @@ Worker::runAll();
|
|
|
```
|
|
|
|
|
|
### An http server
|
|
|
-test.php
|
|
|
```php
|
|
|
-require_once './Workerman/Autoloader.php';
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
use Workerman\Worker;
|
|
|
|
|
|
// #### http worker ####
|
|
|
@@ -80,9 +77,8 @@ Worker::runAll();
|
|
|
```
|
|
|
|
|
|
### A WebServer
|
|
|
-test.php
|
|
|
```php
|
|
|
-require_once './Workerman/Autoloader.php';
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
use Workerman\WebServer;
|
|
|
use Workerman\Worker;
|
|
|
|
|
|
@@ -100,9 +96,8 @@ Worker::runAll();
|
|
|
```
|
|
|
|
|
|
### A tcp server
|
|
|
-test.php
|
|
|
```php
|
|
|
-require_once './Workerman/Autoloader.php';
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
use Workerman\Worker;
|
|
|
|
|
|
// #### create socket and listen 1234 port ####
|
|
|
@@ -168,9 +163,8 @@ class MyTextProtocol
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-test.php
|
|
|
```php
|
|
|
-require_once './Workerman/Autoloader.php';
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
use Workerman\Worker;
|
|
|
|
|
|
// #### MyTextProtocol worker ####
|
|
|
@@ -197,9 +191,8 @@ Worker::runAll();
|
|
|
```
|
|
|
|
|
|
### Timer
|
|
|
-test.php
|
|
|
```php
|
|
|
-require_once './Workerman/Autoloader.php';
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
use Workerman\Worker;
|
|
|
use Workerman\Lib\Timer;
|
|
|
|
|
|
@@ -220,9 +213,150 @@ $task->onWorkerStart = function($task)
|
|
|
Worker::runAll();
|
|
|
```
|
|
|
|
|
|
-run with:
|
|
|
+### AsyncTcpConnection
|
|
|
+```php
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
+use Workerman\Worker;
|
|
|
+use Workerman\Connection\AsyncTcpConnection;
|
|
|
+
|
|
|
+$worker = new Worker();
|
|
|
+$worker->onWorkerStart = function()
|
|
|
+{
|
|
|
+ // Websocket protocol for client.
|
|
|
+ $ws_connection = new AsyncTcpConnection("ws://echo.websocket.org:80");
|
|
|
+ $ws_connection->onConnect = function($connection){
|
|
|
+ $connection->send('hello');
|
|
|
+ };
|
|
|
+ $ws_connection->onMessage = function($connection, $data){
|
|
|
+ echo "recv: $data\n";
|
|
|
+ };
|
|
|
+ $ws_connection->onError = function($connection, $code, $msg){
|
|
|
+ echo "error: $msg\n";
|
|
|
+ };
|
|
|
+ $ws_connection->onClose = function($connection){
|
|
|
+ echo "connection closed\n";
|
|
|
+ };
|
|
|
+ $ws_connection->connect();
|
|
|
+};
|
|
|
+Worker::runAll();
|
|
|
+```
|
|
|
+
|
|
|
+### Work with Async Mysql of ReactPHP
|
|
|
+```
|
|
|
+composer require react/mysql
|
|
|
+```
|
|
|
+
|
|
|
+```php
|
|
|
+<?php
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
+use Workerman\Worker;
|
|
|
+
|
|
|
+$worker = new Worker('tcp://0.0.0.0:6161');
|
|
|
+$worker->onWorkerStart = function() {
|
|
|
+ global $mysql;
|
|
|
+ $loop = Worker::getEventLoop();
|
|
|
+ $mysql = new React\MySQL\Connection($loop, array(
|
|
|
+ 'host' => '127.0.0.1',
|
|
|
+ 'dbname' => 'dbname',
|
|
|
+ 'user' => 'user',
|
|
|
+ 'passwd' => 'passwd',
|
|
|
+ ));
|
|
|
+ $mysql->on('error', function($e){
|
|
|
+ echo $e;
|
|
|
+ });
|
|
|
+ $mysql->connect(function ($e) {
|
|
|
+ if($e) {
|
|
|
+ echo $e;
|
|
|
+ } else {
|
|
|
+ echo "connect success\n";
|
|
|
+ }
|
|
|
+ });
|
|
|
+};
|
|
|
+$worker->onMessage = function($connection, $data) {
|
|
|
+ global $mysql;
|
|
|
+ $mysql->query('show databases' /*trim($data)*/, function ($command, $mysql) use ($connection) {
|
|
|
+ if ($command->hasError()) {
|
|
|
+ $error = $command->getError();
|
|
|
+ } else {
|
|
|
+ $results = $command->resultRows;
|
|
|
+ $fields = $command->resultFields;
|
|
|
+ $connection->send(json_encode($results));
|
|
|
+ }
|
|
|
+ });
|
|
|
+};
|
|
|
+Worker::runAll();
|
|
|
+```
|
|
|
+
|
|
|
+### Work with Async Redis of ReactPHP
|
|
|
+```
|
|
|
+composer require clue/redis-react
|
|
|
+```
|
|
|
+
|
|
|
+```php
|
|
|
+<?php
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
+use Clue\React\Redis\Factory;
|
|
|
+use Clue\React\Redis\Client;
|
|
|
+use Workerman\Worker;
|
|
|
|
|
|
-```php test.php start```
|
|
|
+$worker = new Worker('tcp://0.0.0.0:6161');
|
|
|
+
|
|
|
+$worker->onWorkerStart = function() {
|
|
|
+ global $factory;
|
|
|
+ $loop = Worker::getEventLoop();
|
|
|
+ $factory = new Factory($loop);
|
|
|
+};
|
|
|
+
|
|
|
+$worker->onMessage = function($connection, $data) {
|
|
|
+ global $factory;
|
|
|
+ $factory->createClient('localhost:6379')->then(function (Client $client) use ($connection) {
|
|
|
+ $client->set('greeting', 'Hello world');
|
|
|
+ $client->append('greeting', '!');
|
|
|
+
|
|
|
+ $client->get('greeting')->then(function ($greeting) use ($connection){
|
|
|
+ // Hello world!
|
|
|
+ echo $greeting . PHP_EOL;
|
|
|
+ $connection->send($greeting);
|
|
|
+ });
|
|
|
+
|
|
|
+ $client->incr('invocation')->then(function ($n) use ($connection){
|
|
|
+ echo 'This is invocation #' . $n . PHP_EOL;
|
|
|
+ $connection->send($n);
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+Worker::runAll();
|
|
|
+```
|
|
|
+
|
|
|
+### Work with Aysnc dns of ReactPHP
|
|
|
+```
|
|
|
+composer require react/dns
|
|
|
+```
|
|
|
+
|
|
|
+```php
|
|
|
+require_once __DIR__ . '/vendor/autoload.php';
|
|
|
+use Workerman\Worker;
|
|
|
+$worker = new Worker('tcp://0.0.0.0:6161');
|
|
|
+$worker->onWorkerStart = function() {
|
|
|
+ global $dns;
|
|
|
+ // Get event-loop.
|
|
|
+ $loop = Worker::getEventLoop();
|
|
|
+ $factory = new React\Dns\Resolver\Factory();
|
|
|
+ $dns = $factory->create('8.8.8.8', $loop);
|
|
|
+};
|
|
|
+$worker->onMessage = function($connection, $host) {
|
|
|
+ global $dns;
|
|
|
+ $host = trim($host);
|
|
|
+ $dns->resolve($host)->then(function($ip) use($host, $connection) {
|
|
|
+ $connection->send("$host: $ip");
|
|
|
+ },function($e) use($host, $connection){
|
|
|
+ $connection->send("$host: {$e->getMessage()}");
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+Worker::runAll();
|
|
|
+```
|
|
|
|
|
|
## Available commands
|
|
|
```php test.php start ```
|
|
|
@@ -325,7 +459,12 @@ Percentage of the requests served within a certain time (ms)
|
|
|
```
|
|
|
|
|
|
|
|
|
-# Demos
|
|
|
+## other links with workerman
|
|
|
+
|
|
|
+## [PHPSocket.IO](https://github.com/walkor/phpsocket.io)
|
|
|
+[Live demo](http://www.workerman.net/demos/phpsocketio-chat/)
|
|
|
+[Source code](https://github.com/walkor/phpsocket.io)
|
|
|
+
|
|
|
|
|
|
## [tadpole](http://kedou.workerman.net/)
|
|
|
[Live demo](http://kedou.workerman.net/)
|
|
|
@@ -359,11 +498,6 @@ Percentage of the requests served within a certain time (ms)
|
|
|
[Source code](https://github.com/walkor/workerman-chat)
|
|
|

|
|
|
|
|
|
-## [PHPSocket.IO](https://github.com/walkor/phpsocket.io)
|
|
|
-[Live demo](http://www.workerman.net/demos/phpsocketio-chat/)
|
|
|
-[Source code](https://github.com/walkor/phpsocket.io)
|
|
|
-
|
|
|
-
|
|
|
## [statistics](http://www.workerman.net:55757/)
|
|
|
[Live demo](http://www.workerman.net:55757/)
|
|
|
[Source code](https://github.com/walkor/workerman-statistics)
|