2013-06-23 179 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