paho.mqtt.client.WebsocketConnectionError:WebSocket握手错误

时间:2020-10-07 14:11:52

标签: python websocket port mosquitto

我正在使用mosquitto 1.6.3并尝试测试我的websocket。另外我还没有安装TLS。

import paho.mqtt.client as paho
import time

broker="IP ADDRESS"
port=9001
sub_topic="house/#"

def on_subscribe(client, userdata, mid, granted_qos):   #create function for callback
   print("subscribed with qos",granted_qos, "\n")
   pass

def on_message(client, userdata, message):
    print("message received  "  ,str(message.payload.decode("utf-8")))

def on_publish(client,userdata,mid):   #create function for callback
   print("data published mid=",mid, "\n")
   pass

def on_disconnect(client, userdata, rc):
   print("client disconnected ok")

client= paho.Client("client-socks",transport='websockets')       #create client object

client.on_subscribe = on_subscribe       #assign function to callback
client.on_publish = on_publish        #assign function to callback
client.on_message = on_message        #assign function to callback
client.on_disconnect = on_disconnect
print("connecting to broker ",broker,"on port ",port)
client.connect(broker,port)           #establish connection
client.loop_start()
print("subscribing to ",sub_topic)
client.subscribe(sub_topic)
time.sleep(3)
client.publish("house/bulb1","on")    #publish
time.sleep(4)

client.disconnect()

这是错误

  File "py1.py", line 36, in <module>
    client.connect(broker,port)           #establish connection
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 941, in connect
    return self.reconnect()
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 1112, in reconnect
    self._websocket_path, self._websocket_extra_headers)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 3580, in __init__
    self._do_handshake(extra_headers)
  File "/usr/local/lib/python2.7/dist-packages/paho/mqtt/client.py", line 3664, in _do_handshake
    raise WebsocketConnectionError("WebSocket handshake error")
paho.mqtt.client.WebsocketConnectionError: WebSocket handshake error

日志

mosquitto -c /etc/mosquitto/mosquitto.conf

1602081421: Loading config file /etc/mosquitto/conf.d/default.conf
1602081421: mosquitto version 1.6.3 starting
1602081421: Config loaded from /etc/mosquitto/mosquitto.conf.
1602081421: Opening ipv4 listen socket on port 1883.
1602081421: Opening ipv6 listen socket on port 1883.
1602081421: Opening websockets listen socket on port 9001.

mosquitto.conf

allow_anonymous true


#mqtt

listener  1883
protocol mqtt

#Configuration for the websockets

listener 9001
protocol websockets

任何帮助都会很棒!

0 个答案:

没有答案