搭建一个基于 Prosody 和 Strophe.js 的聊天服务器可以实现实时聊天功能。以下是详细步骤,从安装 Prosody 到使用 HTML 和 JavaScript 与其通讯。
安装和配置 Prosody XMPP 服务器
-
更新软件包列表并安装 Prosody
在 Debian/Ubuntu 上运行以下命令:
sh复制代码
sudo apt update sudo apt install prosody
-
配置 Prosody
编辑 Prosody 配置文件
lua/etc/prosody/prosody.cfg.lua
,添加你的虚拟主机:复制代码
VirtualHost "example.com" enabled = true
-
创建管理员用户
使用以下命令创建管理员用户:
sh复制代码
sudo prosodyctl adduser admin@example.com
-
启用 WebSocket 模块
在 Prosody 配置文件中启用 WebSocket 支持:
lua复制代码
modules_enabled = { -- Other modules "websocket"; "http"; } consider_bosh_secure = true;
-
重启 Prosody 服务
运行以下命令重启 Prosody:
sh复制代码
sudo systemctl restart prosody
创建与 Prosody 通讯的 HTML 页面
-
下载 Strophe.js
从 GitHub 下载 Strophe.js 并将其包含到你的项目中:
sh复制代码
wget https://raw.githubusercontent.com/strophe/strophejs/master/dist/strophe.js
-
创建 HTML 文件
创建一个 HTML 文件
htmlindex.html
,并添加以下内容:复制代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Chat with Prosody</title> <script src="strophe.js"></script> <script> var connection = null; function onConnect(status) { if (status == Strophe.Status.CONNECTING) { console.log('Strophe is connecting.'); } else if (status == Strophe.Status.CONNFAIL) { console.log('Strophe failed to connect.'); } else if (status == Strophe.Status.DISCONNECTING) { console.log('Strophe is disconnecting.'); } else if (status == Strophe.Status.DISCONNECTED) { console.log('Strophe is disconnected.'); } else if (status == Strophe.Status.CONNECTED) { console.log('Strophe is connected.'); connection.addHandler(onMessage, null, 'message', null, null, null); connection.send($pres().tree()); } } function onMessage(msg) { var from = msg.getAttribute('from'); var body = msg.getElementsByTagName('body')[0]; var message = body ? Strophe.getText(body) : null; if (message) { var chat = document.getElementById('chat'); var p = document.createElement('p'); p.innerHTML = from + ': ' + message; chat.appendChild(p); } return true; } function sendMessage() { var to = document.getElementById('to').value; var message = document.getElementById('message').value; var reply = $msg({to: to, from: connection.jid, type: 'chat'}).c('body').t(message); connection.send(reply.tree()); document.getElementById('message').value = ''; } function connect() { var jid = document.getElementById('jid').value; var password = document.getElementById('password').value; connection = new Strophe.Connection('wss://example.com:5280/xmpp-websocket'); connection.connect(jid, password, onConnect); } </script> </head> <body> <div> <input type="text" id="jid" placeholder="JID"> <input type="password" id="password" placeholder="Password"> <button onclick="connect()">Connect</button> </div> <div> <input type="text" id="to" placeholder="Recipient JID"> <input type="text" id="message" placeholder="Message"> <button onclick="sendMessage()">Send</button> </div> <div id="chat"></div> </body> </html>
运行和测试聊天服务器
-
将 HTML 文件部署到 Web 服务器
确保
strophe.js
和index.html
文件在同一目录下,并将该目录部署到你的 Web 服务器上。 -
访问 HTML 页面
通过浏览器访问部署的
index.html
文件,输入你的 JID 和密码,点击 "Connect" 按钮连接到 Prosody 服务器。 -
发送消息
在连接成功后,输入接收者的 JID 和消息内容,点击 "Send" 按钮发送消息。接收到的消息会显示在页面上的聊天区域。
通过这些步骤,你就可以成功搭建一个基于 Prosody 和 Strophe.js 的聊天服务器,实现实时聊天功能。如果遇到问题,可以查看 Prosody 和 Strophe.js 的文档以获取更多帮助。