首页 > PHP > PHP搭建WebSocket服务端,实现广播+指定用户通讯

PHP搭建WebSocket服务端,实现广播+指定用户通讯

时间:2023-07-30浏览次数:680

1. 选择WebSocket库: PHP本身没有原生支持WebSocket,所以你需要使用第三方WebSocket库。常用的选择是Ratchet(https://github.com/ratchetphp/Ratchet)。你可以通过Composer来安装它。


2. 设置WebSocket服务器: 在你的PHP项目中,创建一个WebSocket服务器脚本。这个脚本将会监听WebSocket连接并处理客户端的消息。


3. 处理连接与断开:当客户端连接到WebSocket服务器时,你需要跟踪连接的用户,并在用户断开连接时进行处理。


4. 实现广播: 要给所有连接的用户推送消息,你可以创建一个广播功能,将消息发送到所有已连接的客户端。


5. 实现点对点推送:要给指定连接的用户推送消息,你可以通过识别每个连接的唯一标识符来实现点对点推送。

下面是一个简单的示例代码,演示了如何使用Ratchet库来搭建一个WebSocket服务端,实现广播和点对点推送:

<?php

// 1. 使用Composer安装Ratchet库
// composer require cboden/ratchet

// 2. 引入Ratchet库
require 'vendor/autoload.php';

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

// 3. 创建WebSocket服务端类
class MyWebSocketServer implements MessageComponentInterface {
    protected $clients;

    public function __construct() {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn) {
        // 新连接加入,保存客户端连接
        $this->clients->attach($conn);
        echo "New connection! ({$conn->resourceId})\n";
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        $data = json_decode($msg, true);
        if ($data['type'] === 'broadcast') {
            // 广播消息给所有连接的客户端
            $this->broadcastMessage($data['message']);
        } elseif ($data['type'] === 'private' && isset($data['recipient'])) {
            // 发送私有消息给特定客户端
            $this->sendPrivateMessage($data['message'], $data['recipient']);
        }
    }

    public function onClose(ConnectionInterface $conn) {
        // 连接断开,从客户端连接列表中移除
        $this->clients->detach($conn);
        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error occurred: {$e->getMessage()}\n";
        $conn->close();
    }

    public function broadcastMessage($message) {
        foreach ($this->clients as $client) {
            $client->send($message);
        }
    }

    public function sendPrivateMessage($message, $recipient) {
        foreach ($this->clients as $client) {
            if ($client->resourceId == $recipient) {
                $client->send($message);
                return;
            }
        }
    }
}

// 4. 启动WebSocket服务器
$server = new \Ratchet\App('127.0.0.1', 2347);
$server->route('/websocket', new MyWebSocketServer, ['*']);
echo "WebSocket server is running...\n";
$server->run();

运行脚本

php 文件名 start


客户端调用:

<!DOCTYPE html>
<html>
<head>
    <title>WebSocket Client</title>
</head>
<body>
    <h1>WebSocket Client</h1>
    <div id="output"></div>

    <script>
        const outputDiv = document.getElementById('output');
        const webSocket = new WebSocket('ws://127.0.0.1:2347/websocket'); // Replace with your WebSocket server address

        webSocket.onopen = () => {
            outputDiv.innerHTML += '<p>WebSocket connection is open.</p>';
        };

        webSocket.onmessage = (event) => {
            const message = event.data;
            outputDiv.innerHTML += '<p>${message}</p>';
        };

        webSocket.onclose = () => {
            outputDiv.innerHTML += '<p>WebSocket connection is closed.</p>';
        };

        function sendMessage() {
            const message = document.getElementById('messageInput').value;
            webSocket.send(JSON.stringify({ type: 'broadcast', message: message }));
        }

        function sendPrivateMessage() {
            const recipientId = document.getElementById('recipientInput').value;
            const message = document.getElementById('privateMessageInput').value;
            webSocket.send(JSON.stringify({ type: 'private', message: message, recipient: recipientId }));
        }
    </script>

    <h2>Send a Broadcast Message</h2>
    <input type="text" id="messageInput" placeholder="Enter message">
    <button onclick="sendMessage()">Send Broadcast</button>

    <h2>Send a Private Message</h2>
    <input type="text" id="recipientInput" placeholder="Recipient ID">
    <input type="text" id="privateMessageInput" placeholder="Enter private message">
    <button onclick="sendPrivateMessage()">Send Private</button>
</body>
</html>