1

Server code in java:

@OnMessage
public void onMessage(Session session, ByteBuffer message) {
    if (session.isOpen()) {
        String msg = new String(message.array());
        System.out.println("Message from " + session.getId() + ": " + msg);
        try {
           session.getBasicRemote().sendBinary(ByteBuffer.wrap("I have got the message".getBytes()));
        } catch (IOException ioe) {
            System.out.println(ioe.toString());
        }
    } else {
        System.out.println("Session is not open");
    }
}

Client code in Javascript:

    webSocket = new WebSocket("ws://192.168.10.1:2525/myChat/chat");
    webSocket.binaryType = 'arraybuffer';
    webSocket.onopen = function(event) {
        updateOutput("Connected!");
        connectBtn.disabled = true;
        sendBtn.disabled = false;
    };

    webSocket.onmessage = function(event) {
        updateOutput(event.data);
    };

Note: Server code works fine when I use it with Web GL client as it is send Binary data. Javascript client works fine when I read String data in Server end (from java code):

@OnMessage
public void onMessage(Session session, String message) {}

Thanks in advice for any comments.

1 Answer 1

0

I've found the solution to the issue.

I have used ByteBuffer.js library to send/read data of ByteBuffer type in JavaScript:

webSocket.binaryType = "arraybuffer";

In the function onmessage for reading data:

var d = event.data;
console.log(d.toString());

In the function send for sending data:

var bb = dcodeIO.ByteBuffer.wrap(text);
webSocket.send(bb.toArrayBiffer());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.