eclipse / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
8.93k stars 2.37k forks source link

paho.mqtt.javascript cannot connect to mosquitto(1.6.4) with websockets enabled #1506

Open lht1 opened 4 years ago

lht1 commented 4 years ago

The procedure of compile and running follows:

  1. libwebsockets & mosquitto libwebsockets:
    # git clone https://github.com/warmcat/libwebsockets.git
    # cd libwebsockets
    # git checkout v3.2.0 -b v3.2.0
    # make && make install

mosquitto:

# git clone https://github.com/eclipse/mosquitto.git
# cd mosquitto
# git checkout v1.6.4 -b v1.6.4
# vim config.mk   // modify: WITH_WEBSOCKETS := yes
# make && make install
  1. paho.mqtt.javascript

    # wget -c https://raw.githubusercontent.com/eclipse/paho.mqtt.javascript/master/src/paho-mqtt.js
  2. javascript in html

    var hostname = window.location.host,
        port = 9001,
        clientId = 'client-test',
        timeout = 5,
        keepAlive = 100,
        cleanSession = false,
        ssl = false,
        // userName = 'ucit',  
        // password = 'ucit',  
        topic = 'mqtt/test';
    
    var client = new Paho.Client(hostname, port, "", clientId); 
    var options = {
        invocationContext: {
            host: hostname,
            port: port,
            path: client.path,
            clientId: clientId
        },
        timeout: timeout,
        keepAliveInterval: keepAlive,
        cleanSession: cleanSession,
        useSSL: ssl,
        // userName: userName,  
        // password: password,  
        onSuccess: onConnect,
        onFailure: function (e) {
            console.log(e);
            s = "{time:" + new Date().Format("yyyy-MM-dd hh:mm:ss") + ", onFailure()}";
            console.log(s);
        }
    };
    
    client.connect(options);  
    function onConnect() {
        console.log("onConnected");
        s = "{time:" + new Date().Format("yyyy-MM-dd hh:mm:ss") + ", onConnected()}";
        console.log(s);
        client.subscribe(topic);
    }
  3. mosquitto configurations:

    /etc/mosquitto/mosquitto.conf:
    port = 1883
    listener = 9001
    protocol websockets
  4. run mqtt server:

    # mosquitto -c /etc/mosquitto/mosquitto.conf -v
  5. Get following error in the Browser:

    errorCode: 8, errorMessage: "AMQJS0008I Socket closed."

When I modify following part code in src/websockets.c, it succeeded:

mosquitto$ git diff src/websockets.c
diff --git a/src/websockets.c b/src/websockets.c
index 8a6d3aa..e286410 100644
--- a/src/websockets.c
+++ b/src/websockets.c
@@ -198,7 +198,7 @@ static int callback_mqtt(struct libwebsocket_context *context,
                                for (i=0; i<db->config->listener_count; i++){
                                        if (db->config->listeners[i].protocol == mp_websockets) {
                                                for (j=0; db->config->listeners[i].ws_protocol[j].name; j++){
-                                                       if (p == &db->config->listeners[i].ws_protocol[j]){
+                                                       if (!strcmp(p->name, db->config->listeners[i].ws_protocol[j].name)){
                                                                mosq->listener = &db->config->listeners[i];
                                                                mosq->listener->client_count++;
                                                        }
ralight commented 4 years ago

Thanks for the detailed description. Could I suggest you use version 1.6.7? Libwebsockets 3.2.0 introduced a breaking changing in behaviour that is worked around in 1.6.7.

lht1 commented 4 years ago

Thanks for your typeset for my comment. Sorry, I can't find 1.6.7 in Mosquitto or Libwebsockets. I understand you mean I should use some older version of libwebsockets? Anyway, I think the comparision between pointers is risky. What do you think?

ralight commented 4 years ago

The source is here: https://mosquitto.org/files/source/mosquitto-1.6.7.tar.gz I don't know what system you are using, so can't comment on binaries.

I don't see why the pointer comparison would be problematic. The pointer remains valid for the lifetime of the broker. If you think it could be, please explain.

unexpand commented 4 years ago

Same issue exist when using docker on CoreOS 2303.3.0 .

persistence true persistence_location /mosquitto/data/ log_dest file /mosquitto/log/mosquitto.log connection_messages true log_timestamp true allow_anonymous true listener 1883 listener 9001 protocol websockets socket_domain ipv4

Followed the instructions on https://hub.docker.com/_/eclipse-mosquitto?tab=description using latest & version 1.6.7, 1.6.8

tcp dump shows 1575827145: Client disconnected due to protocol error. when connecting through Websockets.