Last active
April 29, 2016 15:53
-
-
Save IvanZelenskyy/dfc93e477ba81ba336439c9aba76cc81 to your computer and use it in GitHub Desktop.
Vert.x web-socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import io.vertx.core.Vertx; | |
/** | |
* Created by developer on 29.04.16. | |
*/ | |
public class Main { | |
public static void main(String[] args) { | |
Vertx vertx = Vertx.vertx(); | |
vertx.deployVerticle(new MyWSServerVerticle());//, new DeploymentOptions().setMultiThreaded(true).setWorker(true));//.setInstances()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import io.vertx.core.*; | |
import io.vertx.core.http.HttpServerOptions; | |
import io.vertx.core.http.ServerWebSocket; | |
import io.vertx.core.logging.Logger; | |
import io.vertx.core.logging.LoggerFactory; | |
import io.vertx.ext.web.Router; | |
/** | |
* Created by developer on 29.04.16. | |
*/ | |
public class MyWSServerVerticle extends AbstractVerticle { | |
@Override | |
public void start(Future<Void> startFuture) throws Exception { | |
Logger log = LoggerFactory.getLogger(Main.class); | |
long main_pid = Thread.currentThread().getId(); | |
Handler<ServerWebSocket> wsHandler = serverWebSocket -> { | |
if(!serverWebSocket.path().equalsIgnoreCase("/ws")){ | |
log.info("rejected"); | |
serverWebSocket.reject(); | |
} else { | |
log.info("accepted..."); | |
long socket_pid = Thread.currentThread().getId(); | |
serverWebSocket.handler(buffer -> { | |
String str = buffer.getString(0, buffer.length()); | |
long handler_pid = Thread.currentThread().getId(); | |
log.info("Got ws msg: " + str); | |
//Buffer res = Buffer.buffer().appendString("pong:").appendString(str); | |
//serverWebSocket.write(res); | |
String res = String.format("(req:%s)main:%d sock:%d handlr:%d", str, main_pid, socket_pid, handler_pid); | |
try { | |
Thread.sleep(100); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
serverWebSocket.writeFinalTextFrame(res); | |
}); | |
} | |
}; | |
vertx | |
.createHttpServer(new HttpServerOptions().setUsePooledBuffers(true)) | |
.websocketHandler(wsHandler) | |
.listen(8080); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment