2014-07-16 6 views
0

방금 ​​autobahn을 사용하기 시작했습니다. 이미 몇 가지 예를 들었고 자습서를 기반으로 자체 서버와 클라이언트를 만들려고합니다.파이썬 서버에서 자바 스크립트에 등록 된 호출 프로 시저 Autobahn, Wamp

나는 파이어 폭스 서버와 클라이언트 측 자바 스크립트 라이브러리에 autobahn을 사용할 것이다. 지금은 클라이언트에서 서버로만 프로 시저를 호출 할 수 있지만 그 반대는 수행 할 수 없다는 문제로 고심하고 있으며 클라이언트 측에서만 이벤트를 게시 할 수 있습니다. 내가 오류를

Failure: autobahn.wamp.exception.ApplicationError: 
ApplicationError('wamp.error.no_such_procedure', args = ("no procedure 
'com.myapp.add2client' registered",), kwargs = {}) 

에 따라 얻을 프로 시저를 호출하려고하면

는 그래서 응용 프로그램이 등록되지 않은 것 같다,하지만 내가 precedure ID를 얻을대로하는 것처럼 클라이언트 측에서 그것을 본다.

Procedure registered: 4026531820798724 

누구나 올바른 방향으로 나를 가리킬 수 있습니까?

클라이언트 측

<script> 

    var connection = new autobahn.Connection({ 
    url: 'ws://raspberrypi.local:8080', 
    realm: 'realm1' 
    }); 

    connection.onopen = function (session) { 

     // 1) subscribe to a topic 
     function onevent(args) { 
      console.log("Event:", args[0]); 
     } 
     session.subscribe('com.myapp.hellofromserver', onevent); 

     // 2) publish an event 
     session.publish('com.myapp.hellofromclient', ['Hello, world!']); 

     // 3) register a procedure for remoting 
     function add2(args) { 
      console.log("procedure called, result:", res); 
      return args[0] + args[1]; 
     } 
     session.register('com.myapp.add2client', add2).then(
      function (registration) { 
       console.log("Procedure registered:", registration.id); 
      }, 
      function (error) { 
       console.log("Registration failed:", error); 
      } 
     ); 

     // 4) call a remote procedure 
     session.call('com.myapp.add2server', [10, 10]).then(
      function (res) { 
      console.log("Result:", res); 
      } 
     ); 
    }; 

    connection.open(); 

    </script> 

서버 측

# Imports 

    import sys 


    from twisted.python import log 
    from twisted.internet import reactor 
    from twisted.internet.defer import inlineCallbacks 
    from twisted.internet.endpoints import serverFromString 

    from autobahn.wamp import router, types 
    from autobahn.twisted.util import sleep 
    from autobahn.twisted import wamp, websocket 

    class MyBackendComponent(wamp.ApplicationSession): 

    def onConnect(self): 
     self.join("realm1") 


    @inlineCallbacks 
    def onJoin(self, details): 


     # 1) subscribe to a topic 
     def onevent(msg): 
     print("Got event: {}".format(msg)) 

     yield self.subscribe(onevent, 'com.myapp.hellofromclient') 

     # 2) publish an event 
     self.publish('com.myapp.hellofromserver', 'Hello, world!') 

     # 3) register a procedure for remoting 
     def add2(x, y): 
     return x + y 

     self.register(add2, 'com.myapp.add2server'); 

     # 4) call a remote procedure 
     res = yield self.call('com.myapp.add2client', 2, 3) 
     print("Got result: {}".format(res)) 




    if __name__ == '__main__': 

    ## 0) start logging to console 
    log.startLogging(sys.stdout) 

    ## 1) create a WAMP router factory 
    router_factory = router.RouterFactory() 

    ## 2) create a WAMP router session factory 
    session_factory = wamp.RouterSessionFactory(router_factory) 

    ## 3) Optionally, add embedded WAMP application sessions to the router 
    component_config = types.ComponentConfig(realm = "realm1") 
    component_session = MyBackendComponent(component_config) 
    session_factory.add(component_session) 

    ## 4) create a WAMP-over-WebSocket transport server factory 
    transport_factory = websocket.WampWebSocketServerFactory(session_factory, \ 
                  debug = True, \ 
                  debug_wamp = False) 

    ## 5) start the server from a Twisted endpoint 
    server = serverFromString(reactor, "tcp:8080") 
    server.listen(transport_factory) 

    ## 6) now enter the Twisted reactor loop 
    reactor.run() 

예외

Unhandled error in Deferred: 
Unhandled Error 
Traceback (most recent call last): 
Failure: autobahn.wamp.exception.ApplicationError: ApplicationError('wamp.error.no_such_procedure', args = ("no procedure 'com.myapp.add2client' registered",), kwargs = {}) 
Got event: Hello, my dear server! 

고마워요!

답변

0

백엔드 구성 요소가 라우터에 연결되어 com.myapp.add2client을 호출하려고 할 때 클라이언트 (브라우저)가 아직 연결되지 않아 프로 시저가 아직 등록되지 않았습니다.

독립적으로 실행되는 WAMP 라우터에 대해 프런트 엔드 및 백엔드 구성 요소를 모두 실행하는 경우 구성 요소 시작 순서를 제어 할 수 있습니다. 임베디드 라우터를 따라 백엔드 구성 요소를 실행하면 백엔드 구성 요소가 연결할 수있는 프론트 엔드 구성 요소보다 먼저 시작됩니다.

+0

좋아, 알았어. 고마워요! – user1532132