1

I am trying to connect a website to my java plugin (in minecraft) and let them communicate and play audio. When I try to send a connected message I receive weird characters, how can I fix this??

���}+��X������R�}

Websocket code:

      var ws = new WebSocket("ws://62.210.46.135:40050/");

  var url_string = window.location.href;
  var url = new URL(url_string);
  var c = url.searchParams.get("id");

  ws.onopen = function() {
    ws.send("UUID" + c);
    document.getElementById('IDstatus').innerHTML = "Waiting for response!";
    document.getElementById('IDstatus').style.color = "#2f00ff";
    //document.getElementById('IDstatus').style.color = "#03ad11";
  }

  ws.onmessage = function(e) {
    var data = e.data;
    console.log(e);

    if(data.includes("connected")) {
      document.getElementById('IDstatus').innerHTML = "Connected!";
      document.getElementById('IDstatus').style.color = "#03ad11";
    }
  }

  ws.onerror = function(e) {
    document.getElementById('IDstatus').innerHTML = "ERROR!";
    document.getElementById('IDstatus').style.color = "#ff0022";
  }

Receiver code:

        while(!AudioClient.getClient().isClosed()) {    
        try {
            Socket client = AudioClient.getClient();
            client.setKeepAlive(true);
            BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            String info = null;

            if((info = reader.readLine()) != null){
                System.out.println("Received: " + info + "/n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Justin
  • 39
  • 1
  • 5

1 Answers1

0

Any stream of bytes will have an implicit encoding(it could just be a binary data or base64 encoded or utf8 encoded characters or compressed images and so on)

  1. the decoding charset used by the receiver of data should be same as the encoding charset used by the sender
  2. sender should be sending displayable characters(encoded) in the stream to be displayed for visual representation
  3. Receiver should have the corresponding glyphs installed on the machine to render the appropriate visual representation
Thiyanesh
  • 2,360
  • 1
  • 4
  • 11
  • I have tried adding a Base64 encoder and decoder but that didn't work. Also I removed all ws.send() functions and I noticed that it still sends messages in various lengths. – Justin Feb 11 '21 at 08:40
  • @Justin, base64 encoding is not going to help if the bytes are handled using different character sets on both sides. Base64 is primarily helpful when binary data has to be transported over a text reader. It will better if sender can encode the data in `UTF-8` and receiver can decode the data using `UTF-8`. Currently, there seems to 2 different encodings handled on both ends. – Thiyanesh Feb 11 '21 at 17:05
  • Heya, I fixed it it turned out the be the UTF-8 encoding. Thx for your help! – Justin Feb 12 '21 at 07:43
  • @Justin, Glad it helped – Thiyanesh Feb 12 '21 at 17:19
  • 1
    @aran, Thank you. Have a great day. (I have seen some of my previous comments like `Thank you` have been deleted. May be SO has some auto delete feature for general comments) – Thiyanesh Feb 22 '21 at 04:32