@Override
public void start() {
int port = 9191;
HttpServer server = vertx.createHttpServer();
final SocketIOServer io = new DefaultSocketIOServer(vertx, server);
io.configure(new Configurer() {
public void configure(JsonObject config) {
config.putString("transports", "websocket,flashsocket,xhr-polling,jsonp-polling,htmlfile");
// config.putString("transports", "xhr-polling,jsonp-polling,htmlfile");
}
});
io.sockets().onConnection(new Handler<SocketIOSocket>() {
public void handle(final SocketIOSocket socket) {
io.sockets().emit("this", new JsonObject().putString("will", "be received by everyone"));
socket.on("private message", new Handler<JsonObject>() {
public void handle(JsonObject msg) {
System.out.println("I received " + msg);
}
});
socket.on("msg", new Handler<JsonObject>() {
public void handle(JsonObject event) {
socket.emit("msg", event);
}
});
socket.on("event", new Handler<JsonObject>() {
public void handle(JsonObject event) {
socket.emit("event");
}
});
socket.on("global event", new Handler<JsonObject>() {
public void handle(JsonObject event) {
io.sockets().emit("global event");
}
});
socket.on("message", new Handler<JsonObject>() {
public void handle(JsonObject event) {
String message = event.getString("message");
System.out.println(message);
socket.send(message);
}
});
socket.on("volatile", new Handler<JsonObject>() {
public void handle(JsonObject event) {
socket.volatilize().emit("volatile", new JsonObject().putString("msg", "hello"));
}
});
socket.on("broadcast", new Handler<JsonObject>() {
public void handle(JsonObject event) {
socket.broadcast().emit("broadcast");
}
});
socket.onDisconnect(new Handler<JsonObject>() {
public void handle(JsonObject event) {
System.out.println("disconnect");
io.sockets().emit("user disconnected");
}
});
}
});