keesun / mod-socket-io

Vert.x Socket.IO Module
Other
107 stars 40 forks source link

namespace problem #5

Closed keesun closed 11 years ago

keesun commented 11 years ago

from Vasco Amaral

Sorry again for the "spam". I'm trying to aan example with your module of socket.io for vertx. But not doing well. I think that could be some problems from your module. But I need an answer from you to know if it's my problem or yours module's problem.

I'm trying to create NameSpaces with something like this in javaScript:

var io = require('socket.io').listen(app);
io.sockets.on('connection', function (socket) {
    socket.on('newchannel', function (data) {
        console.log(data);
        onNewNamespace(socket, data.channel, data.sender, io);
    });
});

function onNewNamespace(socket, channel, sender, io) {
   var space = io.of('/' + channel);
    space.on('connection', function (socket) {
        socket.on('message', function (data) {

            space.emit('message', data.data);
        });
    });
}

And running with "node file.js" it's working. but when I change the code to Java like this:

import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServer;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.deploy.Verticle;

  public class VertxSocketIO extends Verticle {

    public void start() {
        System.out.println("Socket IO Started");
        HttpServer server = vertx.createHttpServer();
        SocketIOServer io = new DefaultSocketIOServer(vertx, server);

        io.sockets().onConnection(new Handler<SocketIOSocket>() {
            public void handle(final SocketIOSocket socket) {

                socket.on("newchannel", new Handler<JsonObject>() {

                    public void handle(JsonObject data) {
                        final Namespace newSpace = io.of("/" + data.getField("channel").toString());
                        newSpace.onConnection(new Handler<SocketIOSocket>() {
                            public void handle(SocketIOSocket socket){
                                socket.on("message",  new Handler<JsonObject>() {
                                    public void handle(JsonObject data) {
                                        newSpace.emit("message", data.getObject("data"));
                                    }
                                });
                            }
                        });
                    }
            });
            }
        });

        server.listen(8888);
    }
  }

The functions are the same and do the same features of the javascript example. but my server is not exchanging well my messages. Sometimes server not emit messages but another times it transmits.

Plz if can you answer me it will be great!