本文整理汇总了Java中io.vertx.core.net.NetClient.connect方法的典型用法代码示例。如果您正苦于以下问题:Java NetClient.connect方法的具体用法?Java NetClient.connect怎么用?Java NetClient.connect使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.vertx.core.net.NetClient
的用法示例。
在下文中一共展示了NetClient.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: start
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Override
public void start(Handler<AsyncResult<Void>> startFuture) {
if (port > 0) {
// fail if port is already in use
NetClientOptions options = new NetClientOptions().setConnectTimeout(200);
NetClient c = vertx.createNetClient(options);
c.connect(port, "localhost", res -> {
if (res.succeeded()) {
NetSocket socket = res.result();
socket.close();
startFuture.handle(Future.failedFuture("port " + port + " already in use"));
} else {
start2(startFuture);
}
});
} else {
start2(startFuture);
}
}
示例2: waitPortToClose
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
private void waitPortToClose(Handler<AsyncResult<Void>> stopFuture, int iter) {
if (port > 0) {
// fail if port is already in use
NetClientOptions options = new NetClientOptions().setConnectTimeout(50);
NetClient c = vertx.createNetClient(options);
c.connect(port, "localhost", res -> {
if (res.succeeded()) {
NetSocket socket = res.result();
socket.close();
if (iter > 0) {
vertx.setTimer(100, x -> waitPortToClose(stopFuture, iter - 1));
} else {
stopFuture.handle(Future.failedFuture("port " + port + " not shut down"));
}
} else {
stopFuture.handle(Future.succeededFuture());
}
});
} else {
stopFuture.handle(Future.succeededFuture());
}
}
示例3: noConnectSent
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Test
public void noConnectSent(TestContext context) {
NetClient client = this.vertx.createNetClient();
Async async = context.async();
client.connect(MQTT_SERVER_PORT, MQTT_SERVER_HOST, done -> {
if (done.succeeded()) {
done.result().closeHandler(v -> {
log.info("No CONNECT sent in " + MQTT_TIMEOUT_ON_CONNECT + " secs. Closing connection.");
async.complete();
});
} else {
context.fail();
}
});
// check that the async is completed (so connection was closed by server) in
// the specified timeout (+500 ms just for being sure)
async.await(500 + MQTT_TIMEOUT_ON_CONNECT * 1000);
if (!async.isCompleted())
context.fail();
}
示例4: testSendMessageWithReplyBacktrack
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Test
public void testSendMessageWithReplyBacktrack(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals("Hello vert.x", frame.getJsonObject("body").getString("value"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "hello", "#backtrack", new JsonObject().put("value", "vert.x"), socket);
});
}
示例5: testSendMessageWithDuplicateReplyID
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Test
public void testSendMessageWithDuplicateReplyID(TestContext context) {
// replies must always return to the same origin
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
vertx.eventBus().consumer("third-party-receiver", msg -> context.fail());
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "hello", "third-party-receiver", new JsonObject().put("value", "vert.x"), socket);
});
}
示例6: testSendVoidMessage
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Test
public void testSendVoidMessage(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
vertx.eventBus().consumer("test", (Message<JsonObject> msg) -> {
client.close();
async.complete();
});
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
FrameHelper.sendFrame("send", "test", new JsonObject().put("value", "vert.x"), socket);
});
}
示例7: testSendsFromOtherSideOfBridge
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Test
public void testSendsFromOtherSideOfBridge(TestContext context) {
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals(true, frame.getBoolean("send"));
context.assertEquals("hi", frame.getJsonObject("body").getString("value"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("register", "ping", null, socket);
});
}
示例8: testSendMessageWithReplyBacktrack
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Test
public void testSendMessageWithReplyBacktrack(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals(true, frame.getBoolean("send"));
context.assertEquals("Hello vert.x", frame.getJsonObject("body").getString("value"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("send", "hello", "#backtrack", new JsonObject().put("value", "vert.x"), socket);
});
}
示例9: testSendPing
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Test
public void testSendPing(TestContext context) {
NetClient client = vertx.createNetClient();
final Async async = context.async();
eventHandler = event -> {
if (event.type() == BridgeEventType.SOCKET_PING) {
async.complete();
}
};
client.connect(7000, "localhost", context.asyncAssertSuccess(socket -> {
socket.handler(buff -> {
});
FrameHelper.sendFrame("register", "echo", null, socket);
FrameHelper.sendFrame("ping", socket);
}));
}
示例10: check
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Override
public Future<Result> check() {
Future<Result> future = Future.future();
NetClientOptions options = new NetClientOptions().setConnectTimeout(500);
NetClient client = vertx.createNetClient(options);
client.connect(port, host, res -> {
if (res.succeeded()) {
future.complete(Result.healthy());
} else {
future.complete(Result.unhealthy(res.cause()));
}
client.close();
});
return future;
}
示例11: check
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Override
public CompletableFuture<Result> check() {
VertxCompletableFuture<Result> result = new VertxCompletableFuture<>(vertx);
NetClientOptions options = new NetClientOptions().setConnectTimeout(500);
NetClient client = vertx.createNetClient(options);
client.connect(port, host, res -> {
if (res.succeeded()) {
result.complete(Result.healthy());
} else {
result.complete(Result.unhealthy(res.cause()));
}
client.close();
});
return result;
}
示例12: execute
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Override
public GremlinClient execute(String json, Handler<Buffer> handler) {
NetClient netClient = vertx.createNetClient();
netClient.connect(port, host, res -> {
if (res.succeeded()) {
NetSocket socket = res.result();
socket.write(json).handler(handler);
}
});
return this;
}
示例13: tryConnect
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
private void tryConnect(Handler<AsyncResult<Void>> startFuture, int count) {
NetClientOptions options = new NetClientOptions().setConnectTimeout(MILLISECONDS);
NetClient c = vertx.createNetClient(options);
c.connect(port, "localhost", res -> {
if (res.succeeded()) {
logger.info("Connected to service at port " + port + " count " + count);
NetSocket socket = res.result();
socket.close();
try {
p.getErrorStream().close();
} catch (Exception e) {
logger.error("Closing streams failed: " + e);
}
startFuture.handle(Future.succeededFuture());
} else if (!p.isAlive() && p.exitValue() != 0) {
logger.warn("Service returned with exit code " + p.exitValue());
startFuture.handle(Future.failedFuture("Service returned with exit code "
+ p.exitValue()));
} else if (count < maxIterations) {
vertx.setTimer((long) (count + 1) * MILLISECONDS,
id -> tryConnect(startFuture, count + 1));
} else {
startFuture.handle(Future.failedFuture("Deployment failed. "
+ "Could not connect to port " + port + ": " + res.cause().getMessage()));
}
});
}
示例14: connectToRemote
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
private void connectToRemote(String addr, int port) {
// 5s timeout.
NetClientOptions options = new NetClientOptions().setConnectTimeout(5000);
NetClient client = mVertx.createNetClient(options);
client.connect(port, addr, res -> { // connect handler
if (!res.succeeded()) {
log.error("Failed to connect " + addr + ":" + port + ". Caused by " + res.cause().getMessage());
destory();
return;
}
mTargetSocket = res.result();
setFinishHandler(mTargetSocket);
mTargetSocket.handler(buffer -> { // remote socket data handler
try {
byte [] data = buffer.getBytes();
byte [] encryptData = mCrypto.encrypt(data, data.length);
flowControl(mClientSocket, mTargetSocket);
mClientSocket.write(Buffer.buffer(encryptData));
}catch(CryptoException e){
log.error("Catch exception", e);
destory();
}
});
if (mBufferQueue.length() > 0) {
handleStageData();
}
});
}
示例15: testRegister
import io.vertx.core.net.NetClient; //导入方法依赖的package包/类
@Test
public void testRegister(TestContext context) {
// Send a request and get a response
NetClient client = vertx.createNetClient();
final Async async = context.async();
client.connect(7000, "localhost", conn -> {
context.assertFalse(conn.failed());
NetSocket socket = conn.result();
// 1 reply will arrive
// MESSAGE for echo
final FrameParser parser = new FrameParser(parse -> {
context.assertTrue(parse.succeeded());
JsonObject frame = parse.result();
context.assertNotEquals("err", frame.getString("type"));
context.assertEquals("Vert.x", frame.getJsonObject("body").getString("value"));
client.close();
async.complete();
});
socket.handler(parser);
FrameHelper.sendFrame("register", "echo", null, socket);
// now try to publish a message so it gets delivered both to the consumer registred on the startup and to this
// remote consumer
FrameHelper.sendFrame("publish", "echo", new JsonObject().put("value", "Vert.x"), socket);
});
}