Create and Run a Workerman Project
What is Workerman?
Workerman is a high-performance PHP asynchronous network communication library designed for developing high-concurrency, high-performance network applications. It can be used to build web servers, WebSocket servers, TCP/UDP servers, and more, suitable for scenarios such as real-time chat, game servers, IoT, etc. Workerman is lightweight, easy-to-use, and efficient, enabling developers to easily implement complex network applications.
Key Features and Advantages of Workerman
- High Performance: Based on event-driven and asynchronous non-blocking I/O, capable of handling a large number of concurrent connections.
- Multi-Protocol Support: Supports multiple network protocols including HTTP, WebSocket, TCP, UDP, etc.
- Ease of Use: Offers a simple and easy-to-use API, allowing developers to quickly get started.
- Flexible Extension: Can implement more features through plugins and extensions.
- Strong Community Support: Boasts an active developer community and abundant documentation resources.
Workerman helps developers quickly build high-performance network applications suitable for various high-concurrency processing scenarios.
Create and Run a Simple Web Server Using Workerman
In this article, we will demonstrate how to create and run a simple web server using Workerman in the ServBay environment. We will show how to install Workerman, write basic server code, and run the server.
TIP
ServBay recommends placing websites in the /Applications/ServBay/www
directory for easy management.
Install Workerman
Install Composer
ServBay comes with Composer pre-installed; no need for a separate installation.
Create Project Directory
Create a new project directory and navigate to it:
bashcd /Applications/ServBay/www mkdir servbay-workerman-app cd servbay-workerman-app
1
2
3Install Workerman
Use Composer to install Workerman:
bashcomposer require workerman/workerman
1
Write Web Server Code
Create a server.php
file in the project directory and add the following code:
<?php
require __DIR__ . '/vendor/autoload.php';
use Workerman\Worker;
// Create a Web server instance that listens on port 8080
$webServer = new Worker('http://0.0.0.0:8080');
// Start 4 processes to handle requests
$webServer->count = 4;
// Define request handling logic
$webServer->onMessage = function($connection, $request) {
$connection->send('Hello ServBay!');
};
// Run all Worker instances
Worker::runAll();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Run the Web Server
Run the following command in the project directory to start the web server:
php server.php start
After starting, you can visit http://localhost:8080
in your browser and see the page displaying Hello ServBay!
.
Create a WebSocket Server Using Workerman
Besides the web server, Workerman can also be used to create a WebSocket server. Here's a simple WebSocket server example.
Create WebSocket Server Code
Create a
websocket_server.php
file in the project directory and add the following code:php<?php require __DIR__ . '/vendor/autoload.php'; use Workerman\Worker; // Create a WebSocket server instance that listens on port 8081 $wsServer = new Worker('websocket://0.0.0.0:8081'); // Start 4 processes to handle requests $wsServer->count = 4; // Define connection establishment logic $wsServer->onConnect = function($connection) { echo "New connection\n"; }; // Define message reception logic $wsServer->onMessage = function($connection, $data) { $connection->send('Received: ' . $data); }; // Define connection closure logic $wsServer->onClose = function($connection) { echo "Connection closed\n"; }; // Run all Worker instances Worker::runAll();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28Run WebSocket Server
Run the following command in the project directory to start the WebSocket server:
bashphp websocket_server.php start
1After starting, you can use a WebSocket client (such as a WebSocket debugging tool in your browser) to connect to
ws://localhost:8081
and send messages. The server will echo back the received messages.
Create a TCP Server Using Workerman
Workerman can also be used to create a TCP server. Here's a simple TCP server example.
Create TCP Server Code
Create a
tcp_server.php
file in the project directory and add the following code:php<?php require __DIR__ . '/vendor/autoload.php'; use Workerman\Worker; // Create a TCP server instance that listens on port 8082 $tcpServer = new Worker('tcp://0.0.0.0:8082'); // Start 4 processes to handle requests $tcpServer->count = 4; // Define connection establishment logic $tcpServer->onConnect = function($connection) { echo "New connection\n"; }; // Define message reception logic $tcpServer->onMessage = function($connection, $data) { $connection->send('Received: ' . $data); }; // Define connection closure logic $tcpServer->onClose = function($connection) { echo "Connection closed\n"; }; // Run all Worker instances Worker::runAll();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28Run TCP Server
Run the following command in the project directory to start the TCP server:
bashphp tcp_server.php start
1After starting, you can use a TCP client (such as the
telnet
ornc
command) to connect tolocalhost:8082
and send messages. The server will echo back the received messages.
Summary
By following these steps, you have successfully used ServBay to create and run a Workerman project, utilizing Workerman's functionality to create a web server, WebSocket server, and TCP server. Workerman's high performance and ease of use make it ideal for building high-concurrency, high-performance network applications. We hope this article helps you quickly get started with Workerman and apply it to your projects.