如何修复从 websocket 收到的奇怪字符
How can I fix the weird characters I receive from my websocket
我正在尝试将一个网站连接到我的 java 插件(在 minecraft 中)并让它们进行通信和播放音频。当我尝试发送已连接的消息时,我收到了奇怪的字符,我该如何解决这个问题??
���}+��X������R�}
Websocket 代码:
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";
}
收件人代码:
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();
}
}
任何字节流都会有一个隐式编码(它可以只是二进制数据或base64编码或utf8编码的字符或压缩图像等)
- 数据接收方使用的解码字符集应与发送方使用的编码字符集相同
- 发件人应该在流中发送可显示的字符(编码)以显示视觉表示
- 接收器应在机器上安装相应的字形以呈现适当的视觉表示
我正在尝试将一个网站连接到我的 java 插件(在 minecraft 中)并让它们进行通信和播放音频。当我尝试发送已连接的消息时,我收到了奇怪的字符,我该如何解决这个问题??
���}+��X������R�}
Websocket 代码:
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";
}
收件人代码:
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();
}
}
任何字节流都会有一个隐式编码(它可以只是二进制数据或base64编码或utf8编码的字符或压缩图像等)
- 数据接收方使用的解码字符集应与发送方使用的编码字符集相同
- 发件人应该在流中发送可显示的字符(编码)以显示视觉表示
- 接收器应在机器上安装相应的字形以呈现适当的视觉表示