2013-06-23 234 views
0

我試圖使用高速公路python連接到蚊子經紀人。 如果我使用sub.py裏面有這個代碼:autobahn mosquitto服務器聊天

import mosquitto 

def on_connect(mosq, obj, rc): 
    print("rc: "+str(rc)) 

def on_message(mosq, obj, msg): 
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) 

def on_publish(mosq, obj, mid): 
    print("mid: "+str(mid)) 

def on_subscribe(mosq, obj, mid, granted_qos): 
    print("Subscribed: "+str(mid)+" "+str(granted_qos)) 

def on_log(mosq, obj, level, string): 
    print(string) 

mqttc = mosquitto.Mosquitto() 
mqttc.on_message = on_message 
mqttc.on_connect = on_connect 
mqttc.on_publish = on_publish 
mqttc.on_subscribe = on_subscribe 
# Uncomment to enable debug messages 
mqttc.on_log = on_log 
mqttc.connect("localhost", 1883, 60) 
mqttc.subscribe("control", 0) 


rc = 0 
while rc == 0: 
    rc = mqttc.loop() 

print("rc: "+str(rc)) 

它連接到代理和檢索一個客戶端發佈到控制通道的所有消息。

我倒是想以某種方式使用WebSockets的這些消息推到網頁中,我想使用的WebSocket高速公路PY,從這裏修改的例子http://autobahn.ws/python/getstarted#yourfirstserver

我的代碼是這樣的

import sys 
import mosquitto 

from twisted.internet import reactor 
from twisted.python import log 

from autobahn.websocket import WebSocketServerFactory, \ 
           WebSocketServerProtocol, \ 
           listenWS 


class EchoServerProtocol(WebSocketServerProtocol): 
    def on_connect(mosq, obj, rc): 
    print("rc: "+str(rc)) 

    def on_message(mosq, obj, msg): 
     print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) 

    def onMessage(self, msg, binary): 
     print "sending echo:", msg 
     self.sendMessage(msg, binary) 

    def on_publish(mosq, obj, mid): 
     print("mid: "+str(mid)) 

    def on_subscribe(mosq, obj, mid, granted_qos): 
     print("Subscribed: "+str(mid)+" "+str(granted_qos)) 

mqttc = mosquitto.Mosquitto() 
mqttc.on_message = on_message 
mqttc.on_connect = on_connect 
mqttc.on_publish = on_publish 
mqttc.on_subscribe = on_subscribe 
# Uncomment to enable debug messages 
mqttc.on_log = on_log 
mqttc.connect("192.168.2.109", 1883, 60) 
mqttc.subscribe("control", 0) 


rc = 0 
while rc == 0: 
    rc = mqttc.loop() 

print("rc: "+str(rc)) 
if __name__ == '__main__': 

    log.startLogging(sys.stdout) 

    factory = WebSocketServerFactory("ws://192.168.2.109:8899", debug = TRUE) 
    factory.protocol = EchoServerProtocol 
    listenWS(factory) 

    reactor.run() 

但我收到此錯誤,當我嘗試運行它:

[email protected]:~/authobahn# python myserver.py Traceback (most recent call last): File "myserver.py", line 30, in mqttc.on_message = on_message NameError: name 'on_message' is not defined

回答

0

的問題是,您已經定義了on_message()函數的n在EchoServerProtocol類中。這意味着它對全局mqttc變量不可見。你可能想把所有的mqttc代碼放在你的類中,儘管這取決於你實際想要達到的目標。

你可以做類似下面的代碼:

import mosquitto 

class MyMQTTClass: 
    def __init__(self, clientid=None): 
     self._mqttc = mosquitto.Mosquitto() 
     self._mqttc.on_message = self.mqtt_on_message 
     self._mqttc.on_connect = self.mqtt_on_connect 
     self._mqttc.on_publish = self.mqtt_on_publish 
     self._mqttc.on_subscribe = self.mqtt_on_subscribe 

    def mqtt_on_connect(self, mosq, obj, rc): 
     print("rc: "+str(rc)) 

    def mqtt_on_message(self, mosq, obj, msg): 
     print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload)) 

    def mqtt_on_publish(self, mosq, obj, mid): 
     print("mid: "+str(mid)) 

    def mqtt_on_subscribe(self, mosq, obj, mid, granted_qos): 
     print("Subscribed: "+str(mid)+" "+str(granted_qos)) 

    def mqtt_on_log(self, mosq, obj, level, string): 
     print(string) 

    def run(self): 
     self._mqttc.connect("test.mosquitto.org", 1883, 60) 
     self._mqttc.subscribe("$SYS/#", 0) 

     rc = 0 
     while rc == 0: 
      rc = self._mqttc.loop() 
     return rc 


# If you want to use a specific client id, use 
# mqttc = MyMQTTClass("client-id") 
# but note that the client id must be unique on the broker. Leaving the client 
# id parameter empty will generate a random id for you. 
mqttc = MyMQTTClass() 
rc = mqttc.run() 

print("rc: "+str(rc)) 
+0

Thsnk您重播。這是否是問題所在。 – bogdanioanliviu