0

我想从我的 java 客户端与 sockjs 服务器通信。

示例服务器代码:

var http = require('http');
var sockjs = require('sockjs');
var node_static = require('node-static');

// 1. Echo sockjs server
var sockjs_opts = {sockjs_url: "http://cdn.sockjs.org/sockjs-0.3.min.js"};

var sockjs_echo = sockjs.createServer(sockjs_opts);
sockjs_echo.on('connection', function(conn) {
    conn.on('data', function(message) {
        conn.write(message);
    });
});

// 2. Static files server
var static_directory = new node_static.Server(__dirname);

// 3. Usual http stuff
var server = http.createServer();
server.addListener('request', function(req, res) {
    static_directory.serve(req, res);
});
server.addListener('upgrade', function(req,res){
    res.end();
});

sockjs_echo.installHandlers(server, {prefix:'/echo'});

console.log(' [*] Listening on 0.0.0.0:9999' );
server.listen(9999, '0.0.0.0');

我将如何从我的 java 客户端连接和发送/接收数据?

4

1 回答 1

0
permitted.add(new JsonObject()); // Let everything through
SockJSServer sockJSServer = vertx.createSockJSServer(httpServer);
sockJSServer.bridge(new JsonObject().putString("prefix", "/notify").putNumber("heartbeat_period", 40000)
        .putNumber("session_timeout", 50000), permitted, permitted);
httpServer.listen(7777);

final EventBus bus = vertx.eventBus();
Handler<Message<JsonObject>> myHandler = new Handler<Message<JsonObject>>() {
    public void handle(Message<JsonObject> message) {
        LOGGER.info("I received a message ");
        message.reply((new JsonObject()).putString("test","test"));
    }
};

bus.registerHandler("notify.bus.reply", myHandler);
于 2013-11-13T18:21:24.017 回答