You need to know about Class!
Kivy is event loop programming. When you understand how event work you can go with many event driven programming.
Twisted -> Autobahn -> Crossbar.io
Twisted is also reactor. It's event loop! Autobahn is web-socket programming framework. Crossbar.io is WAMP router.
All components is event ! Asynchronous Programming!
Let's start :)
Kivy, main application should start with main.py (It's very important). In main.py we create WAMP component here.
main.py
import kivy
kivy.require('1.8.0')
from kivy.app import App
from kivy.logger import Logger
from kivy.support import install_twisted_reactor
install_twisted_reactor()from twisted.internet import reactor
from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession
from kivy.uix.floatlayout import FloatLayout
class AppController(FloatLayout):
def __init__(self, **kwargs):
super(AppController, self).__init__(**kwargs)
self.wampComponent = kwargs['wampComponent']
def wamp_connect(self):
from autobahn.twisted.wamp import ApplicationRunner
runner = ApplicationRunner(url, realm="realm1")
runner.run(self.wampComponent, start_reactor=False)
def update_system_status(status):
# do whatever on kivy
self.clock_update(status)
class MyComponent(ApplicationSession):
def __init__(self, config=None):
ApplicationSession.__init__(self,config)
Logger.debug("WAMP: Component created")
def onConnect(self):
Logger.debug("WAMP: Transport connected")
self.join(self.config.realm)
def onChallenge(self, challenge):
Logger.debug("WAMP: Authentication challenge received")
def onLeave(self, details):
Logger.debug("WAMP: Session left")
def onDisconnect(self):
Logger.debug("WAMP: Transport disconnected")
@inlineCallbacks
def onJoin(self, details):
Logger.debug("WAMP: Session attached")
def onsystemstatus(status):
Logger.debug("System received - {}".format(status))
if WampApp:
WampApp.root.update_system_status(status)
class MyApp(App):
def build(self):
return AppController(wampComponent=MyComponent)
if __name__ == '__main__' :
WampApp = MyApp()
WampApp().run()
From WAMP Component we can access by call Kivy Application Object (WampApp)
From Kivy we can access WAMP Component by calling WAMP Class (self.wampComponent )
ไม่มีความคิดเห็น:
แสดงความคิดเห็น