0

I am trying to read value received from JavaScript WebSocket using Java I have this JavaScript code:

const socket = new WebSocket("wss://localhost:7999"); // start

socket.addEventListener("open", (event) => {
    socket.send("Hello!");
});

socket.addEventListener("message", (event) => {
    console.log("Received message.");
});

And this Java code:

import java.io.*;
import java.net.*;

class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket s = new ServerSocket(7999);
        Socket client = s.accept();

        System.out.println("I am in!");
        InputStream inputStream = client.getInputStream();

        InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader in = new BufferedReader(reader);
        String readLine;

        while ((readLine = in.readLine()) != null) {
            System.out.println(readLine);
        }
    }
}

However, upon launching the server and client code, I am receiving this value:

I am in!
���
�.isֿ���pO?��-4/P�P����- ���Q�`�:���x���wG�Z��x��v�##�6���,�+̩�0�/̨�$�#�
�   �(�'����=<5/�
}�� localhost�

��http/1.1
3+)�� �-l-h������ۥ n�}�>�zUZ�Ğ�-+

My goal is to read the value Hello, that I have sent using socket.send. Does anyone know how to fix this problem? Thanks.

5
  • I tried this myself and got something similar. ServerSocket isn't set up by default to work with WebSockets. Commented Nov 27, 2021 at 22:03
  • @ControlAltDel Thanks, do you know how to set it up? Commented Nov 27, 2021 at 22:04
  • Strange how you are reading "http/1.1" above. I believe that's supposed to appear before HTTP header data. This is probably because you aren't using a library meant specifically for WebSockets, which use a very specific standardized protocol and don't just send everything as UTF8 bytes. Maybe try this package: github.com/TooTallNate/Java-WebSocket Commented Nov 27, 2021 at 22:06
  • I went with Ratchet, a PHP websocket implementation. There's some information I found on google on how to do this in Java EE 7: learn.vonage.com/blog/2018/10/22/… Commented Nov 27, 2021 at 22:08
  • @ControlAltDel Thanks, I will give it a try. Commented Nov 27, 2021 at 22:16

1 Answer 1

1

ServerSocket is quite low level construct for handling TCP connections. Websocket even though it starts as an HTTP request it requires server to request connection upgrade, there is handshaking, then handling websocket frames, including some internal frames like ping/pong. Unless you plan to implement that entire functionality, I'd suggest you to not use raw ServerSocket but instead use a library that provides websocket support out of the box.

Some options that I could point you to are:

  • netty
  • vert.x
Sign up to request clarification or add additional context in comments.

Comments

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.