javawebsocketserverminecraftsocketserver

How can I fix the weird characters I receive from my websocket


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();
        }
    }

Solution

  • 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