sending data through websocket

1,616 views
Skip to first unread message

Hitesh Aleriya

unread,
Jul 15, 2015, 9:32:50 AM7/15/15
to ve...@googlegroups.com
Hello All,

I am using vert.x 3.0 for my project. I am using web-socket for sending and receiving data between client and server. Following is the code snippet:

Client.java
HttpClient client = vertx.createHttpClient();
client.websocket(8080, "localhost", "/server", websocket -> {
    websocket.handler(data -> {
        System.out.println("Received data " + data.toString("ISO-8859-1")); //how can I receive data from server
        client.close();
    });
    websocket.writeBinaryMessage(Buffer.buffer("Hello world"));
}); 

Server.java
vertx.createHttpServer().websocketHandler(ws -> ws.handler(ws::writeBinaryMessage)).requestHandler(req -> {
    if (req.uri().equals("/server")) req.response().end("Some data");  //how can I send data to client
}).listen(8080);

I took reference from vert.x web-socket example but here I can't understand how can I send data to client from server. Please help me out. 

bytor99999

unread,
Jul 15, 2015, 4:38:32 PM7/15/15
to ve...@googlegroups.com
There are a couple of ways. One is just reply to the Message that the client sent to the server in the first place. This, of course, is when the client sends a message to the server. In the case of events that happen on the server without the client intervention, then need to send to the event to the client. Each socket has a writeHandlerID() so you can use that as an address to eventBus send to. But you have to convert whatever data you have to a Buffer to send it. So something like this, but doesn't have to be exactly. Ours has a Util class that converts any Object to a Map, then we pass the Map to the constructor of JsonObject. Then call encode() on the JsonObject and pass that Json String to the appendString method of Buffer.

void sendToSocket(String writeHandlerID, Object message) {
 
// Convert to Buffer
  Buffer buffer = Buffer.buffer()
 
JsonObject data = new JsonObject(ObjectUtil.toMap(message))
  buffer
.appendString(data.encodePrettily())
 
eventBus.send(
writeHandlerID, buffer)
}


So we just send to the writeHandlerID address and pass the buffer and voila, message sent to the client.

Mark

bytor99999

unread,
Jul 15, 2015, 4:40:05 PM7/15/15
to ve...@googlegroups.com
Unfortunately, the examples in the vertx-examples projects are very rudimentary. They assume WebServer and just echoing stuff back. Nothing elaborate that would demonstrate more of this stuff. But, it really isn't complicated in the end. Typically, learn it the first time, then you know how to go about it, or write a wrapper class to hide it all from your app.

Mark

Hitesh Aleriya

unread,
Jul 16, 2015, 12:11:49 AM7/16/15
to ve...@googlegroups.com
Hello bytor99999,

Thanks for your reply. But, can you please post the complete mechanism to send data from client and receive at server and vice versa. Please help me.

Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

On Thu, Jul 16, 2015 at 2:10 AM, bytor99999 <bytor...@gmail.com> wrote:
Unfortunately, the examples in the vertx-examples projects are very rudimentary. They assume WebServer and just echoing stuff back. Nothing elaborate that would demonstrate more of this stuff. But, it really isn't complicated in the end. Typically, learn it the first time, then you know how to go about it, or write a wrapper class to hide it all from your app.

Mark

--
You received this message because you are subscribed to a topic in the Google Groups "vert.x" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vertx/3u1TwelDV8w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vertx+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/vertx.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/13918d85-0e43-4a84-b8ae-2590afe055c7%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Tim Fox

unread,
Jul 16, 2015, 2:54:31 AM7/16/15
to ve...@googlegroups.com
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.

Hitesh Aleriya

unread,
Jul 16, 2015, 3:59:58 AM7/16/15
to ve...@googlegroups.com
Hello Tim,

Thanks for the reply but I am stuck in getting data at server side. Can you please correct the following code to get data at comments highlighted.

Client.java
HttpClient client = vertx.createHttpClient();
client.websocket(8080, "localhost", "/server", websocket -> {
    websocket.handler(data -> {
        System.out.println("Received data " + data.toString("ISO-8859-1")); //how can I receive data from server
        client.close();
    });
    websocket.writeBinaryMessage(Buffer.buffer("Hello world"));
}); 

Server.java
vertx.createHttpServer().websocketHandler(ws -> ws.handler(ws::writeBinaryMessage)).requestHandler(req -> {
    if (req.uri().equals("/server")) req.response().end("Some data");  //how can I send data to client
}).listen(8080);

I took reference from vert.x web-socket example but here I can't understand how can I send data to client from server. Please help me out. 
Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

Hitesh Aleriya

unread,
Jul 16, 2015, 4:45:06 AM7/16/15
to ve...@googlegroups.com
Also, I found about Reading frames from WebSockets

websocket.frameHandler(frame -> {
  System.out.println("Received a frame of size!"); //Here how can I read a frame
});

Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

Hitesh Aleriya

unread,
Jul 16, 2015, 5:40:40 AM7/16/15
to ve...@googlegroups.com
Thanks bytor99999 and Tim for your help. Finally, I figured out how to send and receive data using vert.x websockets. Thanks! :)

Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

bytor99999

unread,
Jul 16, 2015, 12:35:46 PM7/16/15
to ve...@googlegroups.com
Cool. I realized you probably needed to see code for a LineParser to convert the Buffer that comes in from the client message in the server to the dataHandler.

Mark

Tim Fox

unread,
Jul 16, 2015, 1:37:58 PM7/16/15
to ve...@googlegroups.com
Hi Hitesh,

I'm not sure how I can help you more. The examples I linked to do exactly that - send data from client to server and from server to client.

bytor99999

unread,
Jul 16, 2015, 3:25:36 PM7/16/15
to ve...@googlegroups.com
I think what might be needed is more than just an echo example. As it is quick and easy to do echo without having to deal with Buffer directly. But when you have more work that is done and messages through eventBus before something goes to the client, and it is not a direct request then reply back with the Message request, then it gets more complicated, and would be nice to have one example show that. Basically, have a main Verticle receiving the message from the client. Then putting something on the eventBus to a WorkerVerticle, that is received, and then a brand new JSON is sent back to the writeHandlerID, not via the message.reply() method. That would be really cool.

I am actually about to post a question regarding the message received in now a WebSocket but NetSocket, but almost identical.

Thanks

Mark

Hitesh Aleriya

unread,
Jul 17, 2015, 12:59:42 AM7/17/15
to ve...@googlegroups.com
Hello Guys,


Here, I found out that for server we can use server-keystore.jks file and in Client.java file we use setTrustAll(true), which is not recommended as threat of man in middle attack. So here I am confused about how to get client-keystore.jks file for client or is there any other way to achieve this thing. Can you guys help me in establishing secure socket connection.

Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

Tim Fox

unread,
Jul 17, 2015, 2:32:17 AM7/17/15
to ve...@googlegroups.com
Hi Hitesh,

There's some information here http://vertx.io/docs/vertx-core/java/#ssl

I'd also recommend reading up and getting familiar with the concepts of TLS/SSL including: server certificates and client certificates (none of that is Vert.x specific).

"client-keystore.jks" (I assume you got that from the test suite) is a java key store containing a client certificate. Client certificates are used when the server only allows access from certain clients. Most websites don't use client certificates.

https://en.wikipedia.org/wiki/Client_certificate

I assume your client is a browser (?). Assuming you don't want client certificates, you just want to configure the server with a certificate. The browser needs to decide whether is trusted or not. If the cert is from a trusted root authority (most browsers are preconfigured to trust root authorities) then you don't need to configure anything in the browser. If it's a self signed cert you will need to tell the browser about it or you'll get warnings every time you connect.

Anyway, you can find out all of the above by googling, like I say it's not Vert.x specific :) Good luck!

Hitesh Aleriya

unread,
Jul 17, 2015, 2:37:01 AM7/17/15
to ve...@googlegroups.com
Thanks Tim for your help. But for your information here my client is not browser. My client is separate verticle.

Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

Tim Fox

unread,
Jul 17, 2015, 2:42:13 AM7/17/15
to ve...@googlegroups.com
On 17/07/15 07:36, Hitesh Aleriya wrote:
Thanks Tim for your help. But for your information here my client is not browser. My client is separate verticle.

So, just to clarify:

1. You have a Java Vert.x client and a Java Vert.x server
2. You don't want client certificates (?)

If so:

1. Configure the server with a keystore containing the server certificate and key-  this is the certificate that the server will present to the client during the TLS handshake ("hello client I am XXX, here is my cert to prove it")
2. Configure the client with a trust store containing the server cert. The trust store is the set of server certs that the client trusts.

You can find a test for exactly this case in HttpTest in the core test suite.

Hitesh Aleriya

unread,
Jul 17, 2015, 2:48:36 AM7/17/15
to ve...@googlegroups.com
1. Yes I have Java Vert.x client and a Java Vert.x server.
2. I want to know mechanism for authenticate client with server.

I will try to configure the client with a trust store containing the server cert.

Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

Tim Fox

unread,
Jul 17, 2015, 2:57:14 AM7/17/15
to ve...@googlegroups.com
On 17/07/15 07:47, Hitesh Aleriya wrote:
1. Yes I have Java Vert.x client and a Java Vert.x server.
2. I want to know mechanism for authenticate client with server.

^ That implies you want client certificates.

I'd definitely recommend finding out the differences between server and client certificates so you're sure that's the case.

In most TLS connections its only the *server's* identity which is authenticated, not the clients.

In any case, you can find information on client certificates and how to configure them in the docs, and examples in the test suite that you can copy :)

Hitesh Aleriya

unread,
Jul 18, 2015, 4:02:30 AM7/18/15
to ve...@googlegroups.com
Hello Tim,

I used following code snippets to create my client and server:

Server:
HttpServerOptions serverOptions = new HttpServerOptions();
serverOptions.setHost("localhost");
serverOptions.setPort(4443);
serverOptions.setSsl(true);
serverOptions.setKeyStoreOptions(new JksOptions().setPath("server.jks").setPassword("password"));
HttpServer server = vertx.createHttpServer(serverOptions);

Client:
HttpClientOptions clientOptions = new HttpClientOptions();
clientOptions.setSsl(true);
//clientOptions.setTrustAll(true);  // Code runs fine when I used setTrustAll()
clientOptions.setKeyStoreOptions(new JksOptions().setPath("client.jks").setPassword("password")); // It shows javax.net.ssl.SSLHandshakeException: Failed to create SSL connection
HttpClient client = vertx.createHttpClient(clientOptions);

Now I am getting SSL exception. Do you have any idea what I am missing.

Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

Hitesh Aleriya

unread,
Jul 23, 2015, 8:05:00 AM7/23/15
to ve...@googlegroups.com
I am still facing SSL problem. Can anyone help me.

Hitesh Aleriya
Enterprise Software Engineer
Hotwax Systems
www.hotwaxsystems.com

kiriyam

unread,
Jul 23, 2015, 9:25:15 PM7/23/15
to vert.x, hitesh....@hotwaxsystems.com
Hi Hitesh,

Is the common name of your server cert "localhost"?

If not so, try to call the HttpClientOptions#setVerifyHost(boolean) method with false.

kiriyam.


2015年7月23日木曜日 21時05分00秒 UTC+9 Hitesh Aleriya:
...
Reply all
Reply to author
Forward
0 new messages