Read string received from JavaScript WebSocket using Java InputStreamReader

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.addEventListener("message", (event) => {
    console.log("Received message.");

And this Java code:


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) {

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�

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?

Source: JavaSript – Stack Overflow

November 27, 2021
Category : News
Tags: bufferedwriter | inputstreamreader | java | javascript | websocket

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.