private void testContinuationWriteFromConnectHandler(WebsocketVersion version) throws Exception { String path = "/some/path"; String firstFrame = "AAA"; String continuationFrame = "BBB"; server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .requestHandler( req -> { NetSocket sock = getUpgradedNetSocket(req, path); // Let's write a Text frame raw Buffer buff = Buffer.buffer(); buff.appendByte((byte) 0x01); // Incomplete Text frame buff.appendByte((byte) firstFrame.length()); buff.appendString(firstFrame); sock.write(buff); buff = Buffer.buffer(); buff.appendByte((byte) (0x00 | 0x80)); // Complete continuation frame buff.appendByte((byte) continuationFrame.length()); buff.appendString(continuationFrame); sock.write(buff); }); server.listen( ar -> { assertTrue(ar.succeeded()); client.connectWebsocket( HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> { AtomicBoolean receivedFirstFrame = new AtomicBoolean(); ws.frameHandler( received -> { Buffer receivedBuffer = Buffer.buffer(received.textData()); if (!received.isFinal()) { assertEquals(firstFrame, receivedBuffer.toString()); receivedFirstFrame.set(true); } else if (receivedFirstFrame.get() && received.isFinal()) { assertEquals(continuationFrame, receivedBuffer.toString()); ws.close(); testComplete(); } }); }); }); await(); }
private NetSocket getUpgradedNetSocket(HttpServerRequest req, String path) { assertEquals(path, req.path()); assertEquals("Upgrade", req.headers().get("Connection")); NetSocket sock = req.netSocket(); String secHeader = req.headers().get("Sec-WebSocket-Key"); String tmp = secHeader + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; String encoded = sha1(tmp); sock.write( "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" + "Upgrade: WebSocket\r\n" + "Connection: Upgrade\r\n" + "Sec-WebSocket-Accept: " + encoded + "\r\n" + "\r\n"); return sock; }
private void writeToTarget(String sendPacket, String cmd) { logger.debug(name_log + "writeToDCU(), Command: " + cmd); // Writing Data to The Socket Buffer outBuffer = Buffer.buffer(); outBuffer.setString(0, sendPacket); logger.info(name_log + "SEND[" + outBuffer.length() + "]> \n" + outBuffer.toString() + "\n"); netSocket.write(outBuffer); }
@Test // Let's manually handle the websocket handshake and write a frame to the client public void testHandleWSManually() throws Exception { String path = "/some/path"; String message = "here is some text data"; server = vertx .createHttpServer(new HttpServerOptions().setPort(HttpTestBase.DEFAULT_HTTP_PORT)) .requestHandler( req -> { NetSocket sock = getUpgradedNetSocket(req, path); // Let's write a Text frame raw Buffer buff = Buffer.buffer(); buff.appendByte((byte) 129); // Text frame buff.appendByte((byte) message.length()); buff.appendString(message); sock.write(buff); }); server.listen( ar -> { assertTrue(ar.succeeded()); client .websocket(HttpTestBase.DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path) .exceptionHandler(t -> fail(t.getMessage())) .handler( ws -> { ws.handler( buff -> { assertEquals(message, buff.toString("UTF-8")); testComplete(); }); }); }); await(); }
public void start(Future<Void> startFuture) { logger.info("[SocketCommandController-start()] START! > " + this.name); EventBus eb = vertx.eventBus(); netSocket.handler( new Handler<Buffer>() { @Override public void handle(Buffer inBuffer) { // Reading Data From The Socket logger.debug( name_log + "----------------------------------------------------------------------------"); // logger.debug(name_log + "incoming data: " + inBuffer.length()); String recvPacket = inBuffer.getString(0, inBuffer.length()); // logger.debug(name_log + "read data: " + recvPacket); logger.info(name_log + "RECV[" + recvPacket.length() + "]> \n" + recvPacket + "\n"); // -- Packet Analyze ---------------------------------------------------------------- // Writing received message to event bus JsonObject jo = new JsonObject(); jo.put("recvPacket", recvPacket); eb.send("PD_Trap" + remoteAddr, jo); // -- RESPONSE, Send Packet to Target ----------------------------------------------- eb.consumer( remoteAddr + "Trap_ACK", message -> { writeToTarget(message.body().toString(), "Trap_ACK"); }); eb.consumer( remoteAddr + "Trap_NACK", message -> { writeToTarget(message.body().toString(), "Trap_NACK"); }); } }); }
public SocketCommandController(String name, NetSocket netSocket) { this.name = name; this.name_log = "[" + name + "] "; this.netSocket = netSocket; this.remoteAddr = netSocket.remoteAddress().toString(); }
private Handler<HttpClientResponse> connectHandler(Handler<HttpClientResponse> responseHandler) { Objects.requireNonNull(responseHandler, "no null responseHandler accepted"); return resp -> { HttpClientResponse response; if (resp.statusCode() == 200) { // connect successful force the modification of the ChannelPipeline // beside this also pause the socket for now so the user has a chance to register its // dataHandler // after received the NetSocket NetSocket socket = resp.netSocket(); socket.pause(); response = new HttpClientResponse() { private boolean resumed; @Override public int statusCode() { return resp.statusCode(); } @Override public String statusMessage() { return resp.statusMessage(); } @Override public MultiMap headers() { return resp.headers(); } @Override public String getHeader(String headerName) { return resp.getHeader(headerName); } @Override public String getHeader(CharSequence headerName) { return resp.getHeader(headerName); } @Override public String getTrailer(String trailerName) { return resp.getTrailer(trailerName); } @Override public MultiMap trailers() { return resp.trailers(); } @Override public List<String> cookies() { return resp.cookies(); } @Override public HttpClientResponse bodyHandler(Handler<Buffer> bodyHandler) { resp.bodyHandler(bodyHandler); return this; } @Override public synchronized NetSocket netSocket() { if (!resumed) { resumed = true; vertx .getContext() .runOnContext( (v) -> socket .resume()); // resume the socket now as the user had the chance to // register a dataHandler } return socket; } @Override public HttpClientResponse endHandler(Handler<Void> endHandler) { resp.endHandler(endHandler); return this; } @Override public HttpClientResponse handler(Handler<Buffer> handler) { resp.handler(handler); return this; } @Override public HttpClientResponse pause() { resp.pause(); return this; } @Override public HttpClientResponse resume() { resp.resume(); return this; } @Override public HttpClientResponse exceptionHandler(Handler<Throwable> handler) { resp.exceptionHandler(handler); return this; } }; } else { response = resp; } responseHandler.handle(response); }; }