当前位置:首页 > PHP

php实现在线客服插件

2026-01-29 06:56:43PHP

实现基础功能

使用PHP结合WebSocket技术建立实时通信。安装Ratchet库(PHP WebSocket库),通过Composer进行安装:

composer require cboden/ratchet

创建WebSocket服务器脚本(例如server.php),处理连接、消息收发和断开事件:

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {
    protected $clients;

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

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        foreach ($this->clients as $client) {
            $client->send($msg);
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        $conn->close();
    }
}

$app = new Ratchet\App('localhost', 8080);
$app->route('/chat', new Chat, array('*'));
$app->run();

前端交互界面

创建HTML页面引入JavaScript WebSocket客户端,实现消息发送和接收功能:

<div id="chat-box" style="height:300px;overflow-y:scroll;"></div>
<input type="text" id="message" placeholder="输入消息">
<button onclick="sendMessage()">发送</button>

<script>
    const conn = new WebSocket('ws://localhost:8080/chat');

    conn.onmessage = function(e) {
        document.getElementById('chat-box').innerHTML += 
            '<div>' + e.data + '</div>';
    };

    function sendMessage() {
        const msg = document.getElementById('message').value;
        conn.send(msg);
        document.getElementById('message').value = '';
    }
</script>

用户身份识别

在连接建立时传递用户身份信息,服务器端进行区分存储。修改前端连接代码:

const userId = 'user_' + Math.random().toString(36).substr(2,9);
const conn = new WebSocket('ws://localhost:8080/chat?userId=' + userId);

服务器端解析查询参数并存储用户信息:

public function onOpen(ConnectionInterface $conn) {
    parse_str($conn->httpRequest->getUri()->getQuery(), $query);
    $conn->userId = $query['userId'];
    $this->clients->attach($conn);
}

消息持久化存储

创建MySQL表存储聊天记录:

php实现在线客服插件

CREATE TABLE chat_messages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    sender_id VARCHAR(50),
    message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

在消息处理时插入数据库记录:

public function onMessage(ConnectionInterface $from, $msg) {
    // 存储到数据库
    $pdo = new PDO('mysql:host=localhost;dbname=chat_db', 'username', 'password');
    $stmt = $pdo->prepare("INSERT INTO chat_messages (sender_id, message) VALUES (?, ?)");
    $stmt->execute([$from->userId, $msg]);

    // 广播消息
    foreach ($this->clients as $client) {
        $client->send(json_encode([
            'user' => $from->userId,
            'message' => $msg
        ]));
    }
}

客服人员分配

实现简单的轮询分配策略,创建客服在线列表:

class Chat implements MessageComponentInterface {
    protected $clients;
    protected $supportStaff = [];

    public function onOpen(ConnectionInterface $conn) {
        parse_str($conn->httpRequest->getUri()->getQuery(), $query);
        $conn->userId = $query['userId'];
        $conn->role = $query['role'] ?? 'customer';

        if ($conn->role === 'support') {
            $this->supportStaff[] = $conn;
        } else {
            $this->clients->attach($conn);
        }
    }
}

历史消息加载

添加API端点返回历史消息:

php实现在线客服插件

// 创建HTTP端点
$app->get('/history', function(Request $request) {
    $pdo = new PDO('mysql:host=localhost;dbname=chat_db', 'username', 'password');
    $stmt = $pdo->query("SELECT * FROM chat_messages ORDER BY created_at DESC LIMIT 50");
    return new Response(json_encode($stmt->fetchAll(PDO::FETCH_ASSOC)));
});

前端加载历史记录:

fetch('/history')
    .then(res => res.json())
    .then(messages => {
        messages.reverse().forEach(msg => {
            document.getElementById('chat-box').innerHTML += 
                `<div><strong>${msg.sender_id}:</strong> ${msg.message}</div>`;
        });
    });

运行与部署

启动WebSocket服务器:

php server.php

配置Nginx反向代理WebSocket连接:

location /chat {
    proxy_pass http://localhost:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
}

使用Supervisor保持进程常驻:

[program:chat_server]
command=php /path/to/server.php
autostart=true
autorestart=true
stderr_logfile=/var/log/chat_server.err.log
stdout_logfile=/var/log/chat_server.out.log

分享给朋友:

相关文章

uniapp市场插件

uniapp市场插件

uniapp市场插件概览 uniapp的插件市场(DCloud插件市场)提供了丰富的插件资源,涵盖UI组件、功能扩展、模板、原生插件等类别,帮助开发者快速实现复杂功能。以下是常见插件类型及推荐: U…

vue实现插件功能

vue实现插件功能

Vue 插件实现方法 安装与注册插件 在 Vue 项目中,插件通常以独立模块形式存在。通过 Vue.use() 方法注册插件,该方法会自动调用插件的 install 函数。 // 引入插件 imp…

vue实现图片插件

vue实现图片插件

Vue 图片插件实现方案 在 Vue 中实现图片插件通常涉及图片展示、懒加载、预览等功能。以下是几种常见的实现方式: 基础图片组件封装 创建一个可复用的图片组件,支持默认图片、加载失败处理等基础功能…

vue实现动画插件

vue实现动画插件

Vue 动画插件实现方案 Vue 提供了多种实现动画的方式,可以通过内置的 <transition> 组件或第三方插件来实现复杂的动画效果。 使用 Vue 内置 <transiti…

vue自己实现插件

vue自己实现插件

实现 Vue 插件的基本步骤 Vue 插件通常用于添加全局功能或扩展 Vue 的能力。插件可以包含全局方法、指令、过滤器、混入或组件。 创建一个插件需要定义一个 install 方法,该方法会在插件…

vue实现缩放插件

vue实现缩放插件

Vue 实现缩放插件的方法 使用 vue-zoomable 插件 安装 vue-zoomable 插件: npm install vue-zoomable --save 在 Vue 组件中引入并使用…