如何解决“ Uncaught Reference Error:HelloworldProxy.connect中未定义高速公路”错误?

时间:2019-10-10 05:46:22

标签: javascript autobahn franca

我正在尝试在客户端和wamp c ++服务器上使用js和autobahn实现franca IDL。我完全是这个新手.js,这是我第一次这样做,但是我已经在D-bus上使用franca工作了,所以我对franca有想法。任何帮助和建议都会对您有很大帮助。

在客户端,我编写了一个简单的helloworld代码

package example.hello
  interface HelloWorld {
  version {major 1 minor 0}
  method sayHello {
    in {
      String name
    }
   out {
     String message
   }
  }
} 

并生成了高速公路js绑定代码。现在正在使用如下代码,但是显示以下错误:  “未捕获的ReferenceError:HelloworldProxy.connect中未定义高速公路”

hello.html代码:

         <form>
   <br/><br/><br/>
        <label>Result: <output id="message" /></label><br/>
              <br/><hr/><br/><br/>

      <label>Status: <output id="connectionStatus"></output></label>
     </form>
         <p>This example uses WAMP communication to interact with a C++ 
    server using CommonAPI.</p>
      <br/><br/><br/><hr/>
     <form> 
<br/><br/><br/>
    <label>Result: <output id="message" /></label><br/>
            <br/><hr/><br/><br/>

    <label>Status: <output id="connectionStatus"></output></label>
</form>


 <script src="js/HelloworldProxy.js"></script>
<!--script src="js/HelloworldClientBlueprint.js"></script>-->
<script type="text/javascript">
         console.log("I am alive!");
var proxy = new HelloworldProxy();
proxy.connect('ws://localhost:8081');
            //var clientId = 66;
    //var addr = 
'local:example.hello.helloworld:v0_1:example.hello.helloworld';

proxy.onOpened = function() {
    // your code goes here
 connection.onopen = function(session, details) {
            console.log("Connected", details);
            setStatus("Connected to network.");
};

  /**
  * Async callback in response to a 'sayHello' call.
   * 
   * @param cid the call id
   * @param message
    */
    proxy.replySayHello = function(cid, message) {
    // your code goes here
    };


/**
 * API function provided by the proxy for calling method 'sayHello'.
 * @param name
  */
    proxy.sayHello(name);


};

proxy.onClosed = function(event) {
// your code goes here
            connection.onclose = function(reason, details) {
            console.log("Connection closed", reason, details);
            setStatus("Disconnected.");
         }

};
setStatus("Connecting to server...");
         connection.open();
         function setStatus(text) {
            document.getElementById("connectionStatus").value = text;
         }

      </script>



  </body>
</html>

generated autobahn binding code:

function HelloworldProxy() {
    this.connection = null;
    this.session = null;
    this.address = 
"local:example.hello.helloworld:v0_1:example.hello.helloworld"
    this.callID = 0;
}

HelloworldProxy.prototype.getNextCallID = function() {
    this.callID = this.callID + 1;
    return this.callID;
};

  // call this method to invoke sayHello on the server side
       HelloworldProxy.prototype.sayHello = function(name) {
         var cid = this.getNextCallID();
         var _this = this;
       this.session.call(this.address + '.sayHello', [cid, name]).then(
        function (res) {
           if (typeof(_this.replySayHello) === "function") {
            _this.replySayHello(cid, res);
            }
       },
        function (err) {
            console.log("Call failed, error message: ", err);
            _this.replyError();
        }
    );
    return cid;
};

  HelloworldProxy.prototype.connect = function(address) {
     var _this = this;

_this.connection = new autobahn.Connection({
    url: address,
    realm: 'realm1'}
);

_this.connection.onopen = function(session, details) {
    console.log("Connected", details);
    if (typeof(_this.onOpened) === "function") {
        _this.onOpened();
    }

    _this.session = session;

    // subscribing to all broadcasts
}

_this.connection.onclose = function(reason, details) {
    console.log("Connection closed", reason, details);
    if (typeof(_this.onClosed) === "function") {
        _this.onClosed(reason);
    }
}

setStatus("Connecting to server...");
_this.connection.open();

     };

我需要将客户端与服务器连接,并从服务器获取响应消息。服务器运行良好,我已经在RESTClint.sh的帮助下对其进行了测试。

2 个答案:

答案 0 :(得分:1)

您好,我刚刚在js中添加了显示功能,因此获得了输出。 类似于以下代码:

          document.getElementById("i1").onclick = function(){
          console.log("sending " + Indicator.R_ind);
          proxy.Myindicator(Indicator.name);

         proxy.replySayHello = function(cid, message) {
         document.getElementById("message").value = message;
         document.getElementById("d1").innerHTML = message;
         };

答案 1 :(得分:0)

我努力解决了以上问题的解决方案。我实际上是从高速公路上丢失了autobahn.min.js文件(头文件)。连接已经实现。花了半天的时间才能到达此阶段,但这是一个很好的体验。

现在的问题是我不知道如何在javascript中调用方法sayhello来从服务器获取响应。 即:在javascript部分中,我有

                 /**
                   * Async callback in response to a 'sayHello' call.
                   * 
                   * @param cid the call id
                   * @param message
                     */
                          proxy.replySayHello = function(cid, message) {
                              // your code goes here
                            };


        /**
     * API function provided by the proxy for calling method 'sayHello'.
       * @param name
     */
       proxy.sayHello(name);

我不知道如何调用这些函数,任何建议都会对我有很大帮助。预先感谢。