-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPRxClient.java
More file actions
87 lines (66 loc) · 2.56 KB
/
TCPRxClient.java
File metadata and controls
87 lines (66 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package chhil.vertx.rxexample;
import io.reactivex.Single;
import io.reactivex.observers.DisposableSingleObserver;
import io.vertx.core.Handler;
import io.vertx.reactivex.core.AbstractVerticle;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.core.net.NetSocket;
/**
* @author Murtuza
*
* Make sure to use the io.vertx.reactivex.core.AbstractVerticle;
*/
public class TCPRxClient extends AbstractVerticle {
Single<NetSocket> observableNetSocket;
private boolean connected;
private NetSocket socket;
DisposableSingleObserver<NetSocket> dispSocketObserver;
@Override
public void start() throws Exception {
observableNetSocket = vertx.createNetClient().rxConnect(8888, "127.0.0.1");
// A connect attempt is made only once a subscribe is done.
// On a sunscribe, the client will attempt a connect and will either succeed or fail, if
// it succeeds a NetSocket is returned in the onSuccess part of the lambda , if it fails
// an exception is provided in the onFailure part of the lambda.
observableNetSocket.subscribe(socketOnSuccess -> {
setSocket(socketOnSuccess);
setConnected(true);
}, onFailure -> onFailure.printStackTrace());
}
public NetSocket getSocket() {
return socket;
}
public void setSocket(NetSocket sock) {
this.socket = sock;
}
public boolean isConnected() {
return connected;
}
public void setConnected(boolean connected) {
this.connected = connected;
}
public static void main(String[] args) throws InterruptedException {
TCPRxClient client = new TCPRxClient();
Single<String> deployment = io.vertx.reactivex.core.RxHelper.deployVerticle(Vertx.vertx(), client);
deployment.subscribe(id -> {
// Deployed
System.out.println(id);
}, err -> {
// Could not deploy
err.printStackTrace();
});
Thread.sleep(10000);
if (client.isConnected()) {
byte[] b = { 0, 4, 0x31, 0x32, 0x33, 0x34 };
client.getSocket().write(new String(b));
}
client.getSocket().endHandler(new Handler<Void>() {
@Override
public void handle(Void event) {
System.out.println("DisConnected!");
}
});
Thread.sleep(10000);
client.getSocket().close();
}
}