0

我只是想创建一个 html 页面来获取一个字符串并在同一页面中再次显示它。我的 index.html ,

<html>
<head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>

</head>
<body>
    <ul class="my-maps">
    </ul>
    <h2>Create a Map</h2>
    <form class="create-form">
        <input type="text" id="name" name="name">
        <input type="submit" value="Create">
    </form>
    <script type="text/javascript" src="js/sockjs-0.3.js"></script>
    <script type="text/javascript" src="js/vertxbus-2.1.js"></script>    
    <script type="text/javascript" src="js/jquery-2.1.0.js"></script>    
    <script type="text/javascript" src="js/client.js"></script>
</body>
</html>

当我将浏览器指向 localhost:8080 时,出现这些错误

Firefox can't establish a connection to the server at ws://localhost:8080/eventbus/069/yp3vm_fs/websocket. that.ws = new Constructor(that.url);

The connection to ws://localhost:8080/eventbus/069/yp3vm_fs/websocket was interrupted while the page was loading. that.ws = new Constructor(that.url);

这两个错误都发生在 sockjs-0.3.js 中。

我的client.js:

var eb = null;
  function openConn() {
    if (!eb) {
      eb = new vertx.EventBus("http://localhost:8080/eventbus");
    }
  }

  $(document).ready(function() {
      openConn();
      $('.create-form').submit(function() {
          var value = $("#name").val();
          console.log('name = '+value);
      });
  });

我的java垂直,

public class Mymap extends Verticle{

    public void start() {
        final Logger logger = container.logger();
        logger.info("Starting mymap");
        JsonObject config = new JsonObject();
        config.putNumber("port", 8080);
        config.putString("host", "localhost");
        config.putBoolean("bridge", true);

        JsonArray inboundPermitted = new JsonArray();
        JsonObject addrss1 = new JsonObject().putString("address", "saveMyMap");
        inboundPermitted.add(addrss1);      
        JsonObject addrss2 = new JsonObject().putString("address", "myMaps");
        inboundPermitted.add(addrss2);
        JsonObject addrss3 = new JsonObject().putString("address", "delMyMap");
        inboundPermitted.add(addrss3);

        config.putArray("inbound_permitted", inboundPermitted);

        container.deployModule("io.vertx~mod-web-server~2.0.0-final", config);

        HttpServer server = vertx.createHttpServer();

        server.requestHandler(new Handler<HttpServerRequest>() {
            public void handle(HttpServerRequest request) {
                if (request.path().equals("/")) {
                    request.response().sendFile("./web/index.html");
                }
                if (request.path().endsWith("sockjs-0.3.js")) {
                    request.response().sendFile("./web/js/sockjs-0.3.js");
                }
                if (request.path().endsWith("vertxbus-2.1.js")) {
                    request.response().sendFile("./web/js/vertxbus-2.1.js");
                }
                if (request.path().endsWith("jquery-2.1.0.js")) {
                    request.response().sendFile("./web/js/jquery-2.1.0.js");
                }
                if (request.path().endsWith("client.js")) {
                    request.response().sendFile("./web/js/client.js");
                }
            }
        }).listen(8080, "localhost");
    }
}

我在我的示例中使用 gradle 模板,并使用“gradlew.bat runMod”运行它。

4

1 回答 1

1

您还在https://groups.google.com/forum/#!topic/vertx/ONoF_zN0kAc上提出了您的问题,在那里您获得了制定不同方法的帮助和见解。

mod-web-server关于您在此处发布的问题:当您在同一端口上启动和您自己的 HTTPServer 时,8080它们实际上相互干扰。

您自启动的 HTTPServer 的最小更改为

}).listen(8081, "localhost");

而不是8080已经解决了 WebSocket 连接问题,因为它在同一个端口上很快连接,但由于不一致而崩溃。

最好的

于 2014-07-09T09:22:54.743 回答