Simple Development Examples
Installation
Install Workerman
Run in an empty directory:
composer require workerman/workerman
Example 1: Providing Web Services Using HTTP Protocol
Create start.php file
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;
require_once __DIR__ . '/vendor/autoload.php';
// Create a Worker listening on port 2345, using http protocol for communication
$http_worker = new Worker("http://0.0.0.0:2345");
// Start 4 processes to provide services
$http_worker->count = 4;
// Reply 'hello world' to the browser when data is received
$http_worker->onMessage = function(TcpConnection $connection, Request $request)
{
// Send 'hello world' to the browser
$connection->send('hello world');
};
// Run the worker
Worker::runAll();
Run in Command Line (Windows users use cmd command line, same below)
php start.php start
Test
Assuming the server IP is 127.0.0.1
Visit the URL http://127.0.0.1:2345 in the browser.
Note:
-
If there are access issues, please refer to the reasons for client connection failure section for troubleshooting.
-
The server uses the HTTP protocol and can only communicate with the HTTP protocol; it cannot directly communicate with other protocols such as WebSocket.
Example 2: Providing Services Using WebSocket Protocol
Create ws_test.php file
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
// Note: Unlike the previous example, this uses the websocket protocol
$ws_worker = new Worker("websocket://0.0.0.0:2000");
// Start 4 processes to provide services
$ws_worker->count = 4;
// Return 'hello $data' to the client when data is received
$ws_worker->onMessage = function(TcpConnection $connection, $data)
{
// Send 'hello $data' to the client
$connection->send('hello ' . $data);
};
// Run the worker
Worker::runAll();
Run in Command Line
php ws_test.php start
Test
Open Chrome, press F12 to open the debug console, and enter (or place the following code in an HTML page to run with JS):
// Assuming the server IP is 127.0.0.1
ws = new WebSocket("ws://127.0.0.1:2000");
ws.onopen = function() {
alert("Connection successful");
ws.send('tom');
alert("Send a string to the server: tom");
};
ws.onmessage = function(e) {
alert("Received message from server: " + e.data);
};
Note:
-
If there are access issues, please refer to the manual FAQ - connection failure section for troubleshooting.
-
The server uses the WebSocket protocol and can only communicate with the WebSocket protocol; it cannot directly communicate with other protocols such as HTTP.
Example 3: Transmitting Data Directly Using TCP
Create tcp_test.php
<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';
// Create a Worker listening on port 2347, without using any application layer protocol
$tcp_worker = new Worker("tcp://0.0.0.0:2347");
// Start 4 processes to provide services
$tcp_worker->count = 4;
// When data is received from the client
$tcp_worker->onMessage = function(TcpConnection $connection, $data)
{
// Send 'hello $data' to the client
$connection->send('hello ' . $data);
};
// Run the worker
Worker::runAll();
Run in Command Line
php tcp_test.php start
Test: Run in Command Line
(The following is the effect on the Linux command line, which varies from the effect on Windows)
telnet 127.0.0.1 2347
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^>'.
tom
hello tom
Note:
-
If there are access issues, please refer to the manual FAQ - connection failure section for troubleshooting.
-
The server uses raw TCP protocol and cannot directly communicate with other protocols such as WebSocket or HTTP.