sudo service rabbitmq-server start
rabbitmq-plugins enable rabbitmq_web_mqtt
let mqttjs = "http://localhost:7800/mqttws31.js";
async function LoadLibs() {
try {
await loadScript(mqttjs, _shadowRoot);
} catch (e) {
alert(e);
} finally {
var wsbroker = 'localhost'; //mqtt websocket enabled broker
console.log(wsbroker);
var wsport = 15675; // port for above
client = new Paho.MQTT.Client(wsbroker, wsport, "/ws",
"myclientid_" + parseInt(Math.random() * 100, 10));
console.log(client);
client.onConnectionLost = function(responseObject) {
console.log("CONNECTION LOST - " + responseObject.errorMessage);
};
client.onMessageArrived = function(message) {
console.log("RECEIVE ON " + message.destinationName + " PAYLOAD " + message.payloadString);
if (message.payloadString.split(":")[0] === "loc") {
loadthis(that, changedProperties, message.payloadString);
}
};
var options = {
timeout: 3,
keepAliveInterval: 30,
onSuccess: function() {
console.log("CONNECTION SUCCESS !!");
client.subscribe("events", {
qos: 1
});
},
onFailure: function(message) {
console.log("CONNECTION FAILURE - " + message.errorMessage);
}
};
if (location.protocol == "https:") {
options.useSSL = true;
}
console.log("CONNECT TO " + wsbroker + ":" + wsport);
client.connect(options);
loadthis(that, changedProperties, '');
}
}
LoadLibs();
return Controller.extend("myView.Template", {
onInit: function() {
console.log("-------oninit--------");
console.log("widgetName:" + that.widgetName);
if (that._firstConnection === 0) {
that._firstConnection = 1;
var oData = {
"value": parseInt(that._export_settings.subtitle)
};
var oModel = new JSONModel(oData);
this.getView().setModel(oModel, that.widgetName);
} else {
console.log("After-------------");
console.log(that.widgetName);
MessageToast.show(message.split(":")[1]);
}
},
handleLiveChange: function(oEvent) {
var oTextArea = oEvent.getSource(),
iValueLength = oTextArea.getValue().length,
iMaxLength = oTextArea.getMaxLength(),
sState = iValueLength > iMaxLength ? "Error" : "None";
_input = oTextArea.getValue();
console.log("_input:" + _input);
that._firePropertiesChanged();
this.settings = {};
this.settings.input = "";
that.dispatchEvent(new CustomEvent("onStart", {
detail: {
settings: this.settings
}
}));
oTextArea.setValueState(sState);
var message = new Paho.MQTT.Message("sac:" + _input);
message.destinationName = "events";
console.log("SEND ON " + message.destinationName + " PAYLOAD " + _input);
client.send(message);
}
});
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='SAC', durable=True) #task_queue will be permanent
message = ' '.join(sys.argv[1:]) or "Hello World from SAC"
channel.basic_publish(
exchange='amq.topic',
routing_key='events',
body='loc:' + message,
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
))
print(" [x] Sent %r" % message)
connection.close()
#!/usr/bin/env python
import pika
import time
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='SAC', durable=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
def callback(ch, method, properties, body):
msg = body.decode().split(":")
print(" [x] Received %r" % msg[1])
time.sleep(body.count(b'.'))
print(" [x] Done")
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='SAC', on_message_callback=callback)
channel.start_consuming()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |