groovy - Vert.x 3 与 SockJS - 无法建立连接

标签 groovy websocket sockjs vert.x

我正在尝试使用 Vert.x 3.0.0 和 SockJS 0.3.4(我使用的是 groovy)运行有关 websockets 功能的简单示例。

groovy verticle 看起来像这样:

import io.vertx.groovy.ext.web.Router
import io.vertx.groovy.ext.web.handler.sockjs.SockJSHandler

def router = Router.router(vertx)
def sockJSHandler = SockJSHandler.create(vertx, [heartbeatInterval: 2000])

sockJSHandler.socketHandler({ sockJSSocket ->
    sockJSSocket.handler(sockJSSocket.&write)
})

router.get("/").handler({req -> req.response().sendFile('ws.html')})
router.route("/myapp").handler(sockJSHandler)

vertx.createHttpServer().requestHandler(router.&accept).listen(9090)

在客户端(ws.html):

<html>
<head><title>Web Socket Test</title></head>
<body>
<script src="http://cdn.jsdelivr.net/sockjs/0.3.4/sockjs.min.js"></script>

<script>
    var sock = new SockJS('http://localhost:9090/myapp');

    sock.onopen = function() {
        console.log('open');
    };

    sock.onmessage = function(e) {
        console.log('message', e.data);
    };

    sock.onclose = function() {
        console.log('close');
    };

    function send(message) {
        sock.send(message);
    }
</script>
<form onsubmit="return false;">
    <input type="text" name="message" value="Hello, World!"/>
    <input type="button" value="Send Web Socket Data" onclick="send(this.form.message.value)"/>
</form>
</body>
</html>

但是,当客户端尝试建立连接时,它会请求 URL http://localhost:9090/myapp/info,这会导致 404。我知道它是SockJS协议(protocol),但为什么服务器端verticle不处理它?我应该自己处理“信息”请求吗?

我做错了什么?

最佳答案

如果你看看给定的例子

https://github.com/vert-x3/vertx-examples/blob/master/web-examples/src/main/groovy/io/vertx/example/web/realtime/server.groovy

您需要在通配符地址上注册一个SockJSHandler

关于groovy - Vert.x 3 与 SockJS - 无法建立连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31743161/

相关文章:

java - Spring,SockJS - WebSocket 握手期间出错 : Unexpected response code 400

api - 如何使用 grails 构建数据库抽象层/API/中间件项目,我的看法是否可行?

tsql - Groovy(Java)在TransactSQL数据库(Microsoft SQL Server 2008 R2)中的存储时间以毫秒为单位

google-chrome - 使用 WebSockets 进行端口扫描

node.js - 是否可以让 IRC 客户端使用 websockets 运行您自己的 IRC 服务器而无需 websockets 网关?

migration - 支持 sock.js 上的事件

spring - SockJS 无法创建/连接到 Spring WebSocket

hibernate - 带有抽象类的一对多Grails

常规采取如何重新实现

Python websockets 发送到客户端并保持连接