我正在 hbmqtt 代理上发布一些消息,但是即使我能够在浏览器之间创建连接,但我无法接收消息。我在这里做什么错了?
我对MQTT不太熟悉,因此,我应该感谢任何有趣的事情,在此先感谢您。
所有publisher
代码都可以找到here
我的浏览器脚本
<script src="https://cdnjs.cloudflare.com/ajax/libs/paho-mqtt/1.0.1/mqttws31.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
var mqtt;
var reconnectTimeout = 2000;
var host = "localhost";
var port = 9000;
function onFailure(message) {
console.log("Connection Attempt to Host " + host + "Failed");
setTimeout(MQTTconnect, reconnectTimeout);
}
function onMessageArrived(msg) {
out_msg = "Message received " + msg.payloadString + "<br>";
out_msg = out_msg + "Message received Topic " + msg.destinationName;
console.log(out_msg);
}
function onConnect() {
mqtt.subscribe("bruv");
console.log("Connected ");
}
function MQTTconnect() {
console.log("connecting to " + host + " " + port);
mqtt = new Paho.MQTT.Client(host, port, "");
var options = {
timeout: 3,
onSuccess: onConnect,
onFailure: onFailure,
};
mqtt.onMessageArrived = onMessageArrived
mqtt.connect(options); //connect
}
</script>
<script>
MQTTconnect();
</script>
我的经纪人
mosquito
代理具有默认设置
发布消息
import paho.mqtt.client as mqtt
import json
from datetime import datetime
import time
input_file = open('./data/bus1.json')
json_array = json.load(input_file)
coordinates = json_array['features'][0]['geometry']['coordinates']
client = mqtt.Client(transport='websockets')
client.connect('localhost', 8883)
# Generate all coordinates
def generate_coordinates(coordinates):
# new_coordinates = []
i = 0
while i < len(coordinates):
data = {}
data['busline'] = 201
data['plate'] = 'RAB5674'
data['time_stamp'] = str(datetime.utcnow())
data['longitude'] = coordinates[i][0]
data['latitude'] = coordinates[i][1]
message = json.dumps(data)
client.publish('busline/201', message)
# producer.produce(message.encode('ascii'))
time.sleep(1)
# If buses reaches last coordinaates
if i == len(coordinates)-1:
coordinates = coordinates[::-1]
i = 0
else:
i += 1
# return new_coordinates
generate_coordinates(coordinates)