dimogrudev/php-websocket
最新稳定版本:v1.0.0
Composer 安装命令:
composer require dimogrudev/php-websocket
包简介
No-dependency implementation of WebSocket server in PHP
关键字:
README 文档
README
A zero-dependency native implementation of WebSocket server in PHP, made according to RFC 6455.
Lightweight and minimalistic.
Features
- SSL/TLS encryption.
- Binary and textual data frames, both sending and receiving.
- Control frames (ping/pong/close).
- Built-in non-blocking timers.
Requirements
- PHP 8.4
Important
If you plan to run websockets on a shared hosting, remember that most shared hosting providers block ports for any third-party usage. You will likely have to use a VPS or a dedicated server.
Installation
This library may be installed via Composer:
composer require dimogrudev/php-websocket
Usage
require 'vendor/autoload.php'; // Create an instance of the server class // 0.0.0.0 is set as host to make the server reachable at all IPv4 addresses $server = new WebSocket\Server('0.0.0.0', 8443); // Enable encryption and provide certificate files $server->encryption(true, '../example_le1.crt', '../example_le1.key'); // Print the number of users online every 30 seconds $server->setTimer(function () use ($server): void { print "Current online: {$server->online} user(s)\n"; }, 30000, true); // Handle incoming messages $server->onMessageReceive(function ($client, $message): void { if ($message->binary) { print "{$client->ipAddr} (#{$client->id}) sends binary message ({$message->length} bytes)\n"; } else { print "{$client->ipAddr} (#{$client->id}) sends `{$message->payload}`\n"; } }); $server->start();
Note
Due to their security policies, web browsers won't allow to open a non-secure WebSocket connection (ws:) on a secure website (https:). Using an SSL/TLS certificate (e.g. Let's Encrypt, not a self-signed one) is mandatory in this case.
Timers
// Create a timer to run function repeatedly with a 500 milliseconds interval // It provides timer ID which may be used for later cancellation $timerId = $server->setTimer(function (): void {}, 500, true); // Cancel the timer $server->clearTimer($timerId);
Callbacks
// Server starts $server->onServerStart(function (): void {}); // Server stops $server->onServerStop(function(): void {}); // Client establishes connection and sends handshake request // Return TRUE to accept the request or FALSE to reject it and disconnect the client $server->onClientConnect(function ($client, $request): bool {}); // Client disconnects $server->onClientDisconnect(function ($client): void {}); // Client sends message $server->onMessageReceive(function ($client, $message): void {});
License
The MIT License (MIT). For more information, please see LICENSE.
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-19