Subscribe to browsers using Javascript with MQTT

Asked 1 years ago, Updated 1 years ago, 33 views

At MQTT, I'm thinking about using JavaScript to subscribe to my browser.
JavaScript sources include:
The mqttws31.js file is already in the same directory as index2.html.
Nothing happens on the browser, and you can also connect to the MQTT server from the web console. No action appears to have occurred.

// MQTT Clients
varclient;
// Specify ClientID
varclientId = 'clientid-123456789';
// Connecting to Sango
function connect(){
    var user_name = 'AAAAAA@github';
    var pass = '**********';
    var wsurl='ws://lite.mqtt.shiguredo.jp/mqtt';
    // Create MQTT Client from WebSocketURL and ClientID
    client=new Paho.MQTT.Client(wsurl,clientId);
    // CONNECTION
    client.connect({
        userName —user_name,
        password:pass,
        onSuccess:onConnect,
        onFailure:failConnect
    });
}
// connection failure
functionfailConnect(e){
    console.log('connect failed');
    console.log(e);
}
// Connection Successful
function onConnect() {
    console.log('onConnect');
}

javascript

2022-09-30 20:10

1 Answers

https://eclipse.org/paho/clients/js/

According to
, the sample code appears to be posted. What kind of error are you getting now?

Also, the second argument appears to be the port number setting.

new Paho.MQTT.Client(wsurl,clientId);

in the section of the

new Paho.MQTT.Client(wsurl,Number(location.port),clientId);

It says that

If you are using websocket, you must have an HTTP session in advance.
Note that the location of the websocket is wss:// if it is an https session.

As you seem to be using Sango server, I think it would be better to inquire about specifications based on the server.


2022-09-30 20:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.