• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Ratchet实现PHP WebSocket多人聊天功能的展示

武飞扬头像
芒果芒果丶
帮助1

  

  • composer 安装ratchet
    composer require cboden/ratchet
  • 使用PDO连接数据库,创建mysql命令如下
    1.  
      CREATE TABLE messages (
    2.  
      id INT AUTO_INCREMENT PRIMARY KEY,
    3.  
      message TEXT NOT NULL,
    4.  
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    5.  
      );
  • 使用Redis存储消息列表

这个示例代码中,PHP代码使用Ratchet来创建WebSocket服务器,并实现了简单的聊天功能。HTML代码使用JavaScript来建立WebSocket连接,并处理消息传输和用户输入。要运行此代码,请确保已安装Ratchet并在终端中运行PHP文件。然后,通过打开浏览器并访问HTML代码所在的地址,就可以开始聊天了。

 在onMessage方法中,我们首先将接收到的消息存入Redis列表中。然后,如果Redis中的消息数量超过1000,则将所有消息取出并依次存入MySQL中。请注意,在MySQL中执行多个INSERT语句时,最好使用事务(即BEGIN、COMMIT语句)来确保数据的完整性。

 WebSocket服务端代码:

  1.  
    <?php
  2.  
     
  3.  
    use Ratchet\MessageComponentInterface;
  4.  
    use Ratchet\ConnectionInterface;
  5.  
     
  6.  
    require_once __DIR__ . '/vendor/autoload.php';
  7.  
     
  8.  
    class Chat implements MessageComponentInterface
  9.  
    {
  10.  
    protected $clients;
  11.  
    protected $pdo;
  12.  
    protected $redis;
  13.  
     
  14.  
    public function __construct()
  15.  
    {
  16.  
    $this->clients = new \SplObjectStorage;
  17.  
     
  18.  
    // 连接到数据库
  19.  
    $dsn = 'mysql:host=localhost;dbname=chat';
  20.  
    $username = 'root';
  21.  
    $password = '';
  22.  
    $options = [
  23.  
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
  24.  
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
  25.  
    ];
  26.  
    $this->pdo = new \PDO($dsn, $username, $password, $options);
  27.  
     
  28.  
    // 连接到 Redis
  29.  
    $this->redis = new \Redis();
  30.  
    $this->redis->connect('localhost', 6379);
  31.  
    }
  32.  
     
  33.  
    public function onOpen(ConnectionInterface $conn)
  34.  
    {
  35.  
    $this->clients->attach($conn);
  36.  
    echo "New connection! ({$conn->resourceId})\n";
  37.  
    }
  38.  
     
  39.  
    public function onMessage(ConnectionInterface $from, $msg)
  40.  
    {
  41.  
    foreach ($this->clients as $client) {
  42.  
    if ($from !== $client) {
  43.  
    $client->send($msg);
  44.  
    }
  45.  
    }
  46.  
     
  47.  
    // 将消息存入 Redis
  48.  
    $this->redis->rpush('messages', $msg);
  49.  
     
  50.  
    // 如果 Redis 中的消息数量超过 1000,则将消息存入数据库
  51.  
    if ($this->redis->llen('messages') > 1000) {
  52.  
    $messages = $this->redis->lrange('messages', 0, -1);
  53.  
     
  54.  
    // 开始事务
  55.  
    $this->pdo->beginTransaction();
  56.  
     
  57.  
    foreach ($messages as $message) {
  58.  
    // 将消息存入数据库
  59.  
    $stmt = $this->pdo->prepare('INSERT INTO messages (message) VALUES (?)');
  60.  
    $stmt->execute([$message]);
  61.  
     
  62.  
    // 从 Redis 中删除已经存入数据库的消息
  63.  
    $this->redis->lpop('messages');
  64.  
    }
  65.  
     
  66.  
    // 提交事务
  67.  
    $this->pdo->commit();
  68.  
    }
  69.  
    }
  70.  
     
  71.  
    public function onClose(ConnectionInterface $conn)
  72.  
    {
  73.  
    $this->clients->detach($conn);
  74.  
    echo "Connection {$conn->resourceId} has disconnected\n";
  75.  
    }
  76.  
     
  77.  
    public function onError(ConnectionInterface $conn, \Exception $e)
  78.  
    {
  79.  
    echo "An error has occurred: {$e->getMessage()}\n";
  80.  
    $conn->close();
  81.  
    }
  82.  
    }
  83.  
     
  84.  
    $webSocketServer = new \Ratchet\WebSocket\WsServer(new Chat());
  85.  
    $server = \Ratchet\Server\IoServer::factory(
  86.  
    new \Ratchet\Http\HttpServer($webSocketServer),
  87.  
    8080
  88.  
    );
  89.  
     
  90.  
    $server->run();
学新通

 开启socket服务命令,假设php文件名为socket.php

php ./socket.php

 HTML代码:

  1.  
    <!DOCTYPE html>
  2.  
    <html>
  3.  
    <head>
  4.  
    <meta charset="UTF-8">
  5.  
    <title>WebSocket Chat</title>
  6.  
    </head>
  7.  
    <body>
  8.  
    <div id="messages"></div>
  9.  
    <form>
  10.  
    <input type="text" id="message" placeholder="Enter message">
  11.  
    <button type="submit">Send</button>
  12.  
    </form>
  13.  
     
  14.  
    <script>
  15.  
    var conn;
  16.  
    var connect = function() {
  17.  
    conn = new WebSocket('ws://localhost:8080');
  18.  
     
  19.  
    conn.onopen = function(e) {
  20.  
    console.log("Connection established!");
  21.  
    };
  22.  
     
  23.  
    conn.onmessage = function(e) {
  24.  
    var messages = document.getElementById("messages");
  25.  
    var message = document.createElement("div");
  26.  
    message.innerHTML = e.data;
  27.  
    messages.appendChild(message);
  28.  
    };
  29.  
     
  30.  
    conn.onclose = function(e) {
  31.  
    console.log("Connection closed, attempting to reconnect...");
  32.  
    setTimeout(connect, 1000);
  33.  
    };
  34.  
    };
  35.  
     
  36.  
    connect();
  37.  
     
  38.  
    var form = document.querySelector("form");
  39.  
    var input = document.querySelector("#message");
  40.  
     
  41.  
    form.addEventListener("submit", function(e) {
  42.  
    e.preventDefault();
  43.  
     
  44.  
    conn.send(input.value);
  45.  
    input.value = "";
  46.  
    });
  47.  
    </script>
  48.  
    </body>
  49.  
    </html>
学新通

保证WebSocket服务一直开启,可以使用一个常驻进程管理工具supervisor,使用supervisor的示例配置链接

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgaejki
系列文章
更多 icon
同类精品
更多 icon
继续加载