Websocket using eventBus and textHandlerId - buffer cast exception

201 views
Skip to first unread message

pecksta...@gmail.com

unread,
Jul 23, 2015, 11:19:12 PM7/23/15
to vert.x
I started my test project a few weeks ago and it is coming along nicely.  Today however I read something in the documentation that made me go Duh! and made me realize it is something I should have picked up from the get go.  

Currently I am responding to all incoming messages on the server by using the ServerWebSocket instance directly (keeping all stored in a map for reference later by textHandlerId)  and using websocket.writeFinalBinaryFrame or any other of the write methods.  This works but as I now read that I can just use the eventBus for this by doing EventBus(serverWebsocket.textHandlerId,buffer)... it seems I am doing it the wrong way. Well maybe not the wrong way, but not the most efficient one.


So I tried to do exactly that and use the EventBus.  Here is where I am running into an error though.  For some reason incoming data, in a valid Buffer object, that I try to send out again doesn't make it past the EventBus.send validation and a ClassCastException is thrown.   I also tried many different ways to actually de-serialize the incoming message, re-serialize it again and create a new Buffer.buffer(byte[]) but same result.   Any ideas what might be causing this or maybe something I need to be careful for?   I also tried to replace the input and just sending an empty string and that works perfectly. So its purely a casting Buffer error.    What I am sending is a binary message (coming from c# client, messagePack format).   But I would assume if the data passes the incoming validation, it should not have any problem making its way out again.

This is what I am left with as absolute basic test code, 


Server = vertx.createHttpServer();


 
Server.websocketHandler(websocket -> {


 websocket
.handler(input -> {
     vertx
.eventBus().send(websocket.textHandlerID(),input);
 
});
 
});



and the error

7 24, 2015 11:43:24 午前 io.vertx.core.impl.ContextImpl
重大: Unhandled exception
java.lang.ClassCastException: io.vertx.core.buffer.impl.BufferImpl cannot be cast to java.lang.String
	at io.vertx.core.http.impl.WebSocketImplBase.lambda$new$61(WebSocketImplBase.java:68)
	at io.vertx.core.http.impl.WebSocketImplBase$$Lambda$31/1147788506.handle(Unknown Source)
	at io.vertx.core.eventbus.impl.EventBusImpl$HandlerRegistration.handle(EventBusImpl.java:1108)
	at io.vertx.core.eventbus.impl.EventBusImpl.lambda$doReceive$189(EventBusImpl.java:755)
	at io.vertx.core.eventbus.impl.EventBusImpl$$Lambda$36/1112301788.handle(Unknown Source)
	at io.vertx.core.impl.ContextImpl.lambda$wrapTask$15(ContextImpl.java:314)
	at io.vertx.core.impl.ContextImpl$$Lambda$4/482082765.run(Unknown Source)
	at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:357)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:357)
	at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111)
	at java.lang.Thread.run(Thread.java:745)

Any help welcomed.


 

Julien Viet

unread,
Jul 24, 2015, 4:51:02 AM7/24/15
to ve...@googlegroups.com, pecksta...@gmail.com
convert the buffer to String :

vertx.eventBus().send(websocket.textHandlerID(),input.toString());

-- 
Julien Viet
www.julienviet.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.
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/4ba5d0a5-d6ba-4e8b-ba12-15c30f9f3333%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pecksta...@gmail.com

unread,
Jul 24, 2015, 5:00:27 AM7/24/15
to vert.x, jul...@julienviet.com
Damn, I could have sworn I have tried that.  Seems to be working now.  Maybe I forgot about this working because when sending the data like this my client, messagePack, is not able to deserialize the data anymore.  But I guess it is a messagepack issue that I need to look into and not a vert.x one.   When send like this websocket.writeFinalBinaryFrame(buffer) all is working fine on both server and client.   Either way thanks a lot!
Message has been deleted

pecksta...@gmail.com

unread,
Jul 24, 2015, 5:19:48 AM7/24/15
to vert.x, jul...@julienviet.com, pecksta...@gmail.com
On second thought it is kinda a server issue as I don`t want my my client to receive the data as a string but actual binary format.   I will dive into the sourcecode to see how exactly a message is send  when send over the eventBus.  Meaning using writeFinalTextFrame or writeFinalBinaryFrame. 

pecksta...@gmail.com

unread,
Jul 28, 2015, 2:41:20 AM7/28/15
to vert.x, pecksta...@gmail.com
Figured it out.  Again something simple I overlooked.   Instead of using the websocket.textHandlerId, I just need to use the websocket.binaryHandlerId.     
Reply all
Reply to author
Forward
0 new messages