Current Location: Home> Latest Articles> How to Build a High-Performance Distributed Game Server with PHP (Workerman) and Unity3D

How to Build a High-Performance Distributed Game Server with PHP (Workerman) and Unity3D

M66 2025-06-03

Introduction

As online games demand faster response times and higher reliability, distributed server architecture has become a preferred solution. This article details how to use PHP with the Workerman framework in combination with Unity3D to create a scalable distributed game server, improving both performance and concurrency.

What is Workerman?

Workerman is a high-performance PHP Socket framework designed for building network applications that require high concurrency. Its event-driven, non-blocking I/O model allows it to efficiently handle numerous simultaneous connections. Workerman is lightweight, easy to use, and compatible across platforms, making it ideal for real-time communication, IoT systems, and game servers.

Environment Setup

Before starting development, make sure the following tools are prepared:
  1. PHP environment: Ensure PHP is installed and CLI commands can be executed.
  2. Install Workerman using Composer:
    composer require workerman/workerman
  3. Unity3D development environment: Use a recent version for better compatibility.

Building the Server

1. Create the PHP Server Script

Create a new file named server.php and add the following code:
<?php
require_once __DIR__ . '/vendor/autoload.php';

use Workerman\Worker;

// Create a WebSocket server on port 2345
$worker = new Worker('websocket://0.0.0.0:2345');

// Run 4 processes to handle incoming connections
$worker->count = 4;

// Define message handling behavior
$worker->onMessage = function($connection, $data) {
    // Process client data

    // Send response to client
    $connection->send('Hello, Unity3D!');
};

// Start the worker
Worker::runAll();
?>

This code initializes a WebSocket server using Workerman, ready to handle real-time communication from Unity3D clients.

2. Start the Server

In your terminal, navigate to the directory containing server.php and run:
php server.php

If everything is set up correctly, you’ll see output like:

-------------------------
Workerman starting...
-------------------------
Workerman started...

Unity3D Client Integration

1. Create a Unity Project

Create a new Unity project and import the SocketIO plugin to enable WebSocket communication with the PHP server.

2. Write the Client Script

Inside Unity, create a script named SocketClient.cs and add the following code:
using UnityEngine;
using SocketIO;

public class SocketClient : MonoBehaviour
{
    private SocketIOComponent socket;

    private void Start()
    {
        socket = GetComponent<SocketIOComponent>();

        // Listen for messages from the server
        socket.On("message", OnMessage);

        // Connect to the server
        socket.Connect();

        // Send a message to the server
        socket.Emit("message", "Hello, Server!");
    }

    private void OnMessage(SocketIOEvent e)
    {
        // Log the message from the server
        Debug.Log(e.data.ToString());
    }
}

This script connects to the server, sends a message, and listens for incoming responses from the server.

Testing the Communication

1. Build the Unity Project

Compile the Unity project and ensure the SocketIO plugin is correctly integrated.

2. Run the Client

Launch the Unity client. In the console, you should see the following output:
Hello, Unity3D!

This confirms that the Unity3D client successfully connected to the PHP server and received a response.

Conclusion

This article demonstrated how to use Workerman to create a high-performance distributed PHP server and connect it with a Unity3D client via WebSocket. This kind of architecture is ideal for developing scalable, real-time multiplayer games, offering robust and efficient communication between the client and server. Whether you're building an MMO or a small-scale co-op game, this setup provides a strong foundation for your networking needs.