无法通过Strophe连接本地eJabberd服务器,求排查及可用示例
Let's break down why your connection attempts are failing and fix this step by step:
1. Verify JID Format & eJabberd Host Configuration
First, make sure you're using the full JID format that matches your eJabberd server's configured host. Here's how to confirm:
- Open your eJabberd admin panel at
http://localhost:5280/admin, navigate to the Virtual Hosts section. The listed host (e.g.,localhostorsyed-virtualbox) is the domain part of your JID. - Your login JID must follow the
username@hostpattern: if your virtual host islocalhost, useusr1@localhost; if it'ssyed-virtualbox, useusr1@syed-virtualbox. Partial usernames like justusr1won't work with Strophe.js.
2. Confirm BOSH Service is Enabled & Configured Correctly
Check if eJabberd's BOSH module is active and properly set up:
- Open your
ejabberd.ymlconfiguration file (usually located in/etc/ejabberd/or your local installation directory). - Look for the
modulessection and ensuremod_http_bindis enabled:modules: mod_http_bind: {} - Also verify the HTTP listener is configured to serve the BOSH endpoint. The default setup should include:
If you modified any settings here, restart eJabberd to apply changes.listen: - port: 5280 module: ejabberd_http request_handlers: "/xmpp-httpbind": mod_http_bind
3. Fix Cross-Origin Resource Sharing (CORS) Issues
This is a common gotcha when testing local HTML files directly via the file:// protocol—browsers block cross-origin requests from file:// to http://localhost:5280. Try one of these fixes:
- Option 1: Host the HTML file on a local web server
Run a simple HTTP server in your Strophe.js files directory. For example, with Python 3:
Then access your test page atpython -m http.server 8000http://localhost:8000/basic.htmlinstead of opening the file directly. - Option 2: Add CORS headers to eJabberd
Update yourejabberd.ymlto include CORS headers for the BOSH endpoint. Add this under theejabberd_httplistener:
Restart eJabberd after making this change.listen: - port: 5280 module: ejabberd_http request_handlers: "/xmpp-httpbind": mod_http_bind custom_headers: Access-Control-Allow-Origin: "*" Access-Control-Allow-Methods: "GET, POST, OPTIONS" Access-Control-Allow-Headers: "Content-Type"
4. Working Local Strophe.js Connection Example
Here's a minimal, tested example you can use to replace or supplement the basic.html file:
<!DOCTYPE html> <html> <head> <title>eJabberd Strophe Test</title> <script src="strophe.min.js"></script> </head> <body> <div> <input type="text" id="jid" placeholder="e.g., usr1@localhost" value="usr1@localhost"> <input type="password" id="password" placeholder="Your password"> <button id="connect" onclick="connect()">Connect</button> <button id="disconnect" onclick="disconnect()" disabled>Disconnect</button> </div> <div id="log"></div> <script> var conn; var logDiv = document.getElementById('log'); var connectBtn = document.getElementById('connect'); var disconnectBtn = document.getElementById('disconnect'); function log(message) { logDiv.innerHTML += `<p>${message}</p>`; } function connect() { var jid = document.getElementById('jid').value; var password = document.getElementById('password').value; conn = new Strophe.Connection('http://localhost:5280/xmpp-httpbind'); conn.connect(jid, password, onConnect); } function onConnect(status) { if (status === Strophe.Status.CONNECTED) { log("Successfully connected!"); connectBtn.disabled = true; disconnectBtn.disabled = false; // Send initial presence to notify the server conn.send($pres().tree()); } else if (status === Strophe.Status.DISCONNECTED) { log("Disconnected successfully"); connectBtn.disabled = false; disconnectBtn.disabled = true; } else { log(`Connection failed with status: ${status}`); log("Check your JID, password, or eJabberd configuration"); } } function disconnect() { conn.disconnect(); } </script> </body> </html>
Ensure strophe.min.js is in the same directory as this HTML file, or update the script source path to match your setup.
Final Quick Checks
- Double-check that
usr1andusr2exist in the correct virtual host via the eJabberd admin panel (Users section). - Confirm no local firewall is blocking incoming/outgoing traffic on port 5280.
内容的提问来源于stack exchange,提问作者Zax

