You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过Strophe连接本地eJabberd服务器,求排查及可用示例

Troubleshooting Strophe.js Connection to Local eJabberd & Working Example

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., localhost or syed-virtualbox) is the domain part of your JID.
  • Your login JID must follow the username@host pattern: if your virtual host is localhost, use usr1@localhost; if it's syed-virtualbox, use usr1@syed-virtualbox. Partial usernames like just usr1 won'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.yml configuration file (usually located in /etc/ejabberd/ or your local installation directory).
  • Look for the modules section and ensure mod_http_bind is enabled:
    modules:
      mod_http_bind: {}
    
  • Also verify the HTTP listener is configured to serve the BOSH endpoint. The default setup should include:
    listen:
      - port: 5280
        module: ejabberd_http
        request_handlers:
          "/xmpp-httpbind": mod_http_bind
    
    If you modified any settings here, restart eJabberd to apply changes.

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:
    python -m http.server 8000
    
    Then access your test page at http://localhost:8000/basic.html instead of opening the file directly.
  • Option 2: Add CORS headers to eJabberd
    Update your ejabberd.yml to include CORS headers for the BOSH endpoint. Add this under the ejabberd_http listener:
    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"
    
    Restart eJabberd after making this change.

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 usr1 and usr2 exist 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:51:54