tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Konstantin Kolinko <knst.koli...@gmail.com>
Subject Re: Websocket @OnMessage is never firing for binary messages only text
Date Tue, 18 Nov 2014 20:00:00 GMT
2014-11-18 22:14 GMT+03:00 Jason Ricles <jgr208@alum.lehigh.edu>:
> I am writing a websocket communication with the client and server
> endpoints both in java code (using tomcat 7.0.53 as the web server)
> When I send a text message using the
> session.getbasicremote.sendText(String) method the @Onmessage function
> is fired and everything. However I want to send binary data between
> the websockets so thus have to use
> session.getbasicremote.sendBinary(ByteBuffer). The code should then be
> read within the following method,
>
> @OnMessage
> public void recieved(ByteBuffer byteBuffer)
> {
>     System.out.println(byteBuffer);
> }
>
> However the method is never fired when the message is sent (I debugged
> through both remote debugging and print statements to verify that the
> binary data is being sent also yes regular text does get sent through
> this method when binary is switched to text). Does anyone have a clue
> why this method is never being called when the data is sent from the
> other end of the websocket? Here is also the code for the part where
> the binary data is sent over the websocket. Also the @onError methods
> are in the classes and never called either.
>
> public void SendMessage() throws IOException
> {
>     for(int i = 0;i<MESSAGE_SIZE;i++)
>         message+='\0';
>     for(int i = 0;i<ID_SIZE;i++)
>         id+='\0';
>     ByteBuffer bbuf = ByteBuffer.allocate(1000);
>     bbuf.put(id.getBytes());
>     bbuf.position(33);
>     bbuf.putInt(33,length);
>     bbuf.position(37);
>     bbuf.put(message.getBytes());

1. I think that you are missing bbuf.flip()  here.

>     for(Session session : sessionList)
>         session.getBasicRemote().sendBinary(bbuf);
>     System.out.println("sent");
> }
>

2. There are tests for receiving binary messages in Tomcat,  e.g.
test/org.apache.tomcat.websocket.TesterEchoServer  class.

3. BTW, there is a typo in method name.  s/recieved/received/

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message