ฉันมีปัญหาบางอย่าง ฉันพยายามเขียนเว็บแอปพลิเคชันด้วย websockets สำหรับการสตรีมข้อมูล มันยังไม่พร้อม แต่ฉันกำลังดำเนินการอยู่ ฉันพยายามส่งสตริงเซิร์ฟเวอร์ของฉันและเซิร์ฟเวอร์ควรส่งข้อความกลับมาพร้อมกับ UUID ฉันเพิ่งส่ง UUID ด้วย
session.getBasicRemote().sendText(uuid.tostring())
ใช้งานได้ดี แต่ตอนนี้ฉันพยายามส่งแผนที่เพื่อให้สามารถส่ง UUID และ String เข้าด้วยกันโดยใช้
session.getBasicRemote().sendObject(map);
แต่ตอนนี้ไคลเอนต์ JS ของฉันไม่ได้รับข้อมูลที่ฉันพยายามส่งเซิร์ฟเวอร์ของฉันทำให้เกิดข้อยกเว้น
javax.websocket.EncodeException: ไม่พบตัวเข้ารหัสที่เหมาะสม
ฉันไม่รู้ว่าจะแก้ไขปัญหานี้อย่างไร
รหัสเจเอส
var websocket;
$(document).ready(function(){
websocket = new WebSocket("ws://localhost:8080/TimeStreamingTestartID-1.0-SNAPSHOT/websockets/simplestockmarket")
websocket.onopen = function(){
alert("SUCESS: REGISTERED!!");
}
$("#tblcontainer").html('<table id="tableID" class="table table-bordered" ><thead><tr><th>Identification</th><th>Text</th></tr></thead></table>')
$("#start").click(function(){
websocket.send("Test")
websocket.onmessage = function(erg){
$("#tableID").append('<tr><td>'+"1"+'</td><td>'+erg+'</td></tr>')
console.log(erg);
}
});
});
วิธี java onMessage ของฉัน
@OnMessage
public void message(Session session,String message) throws IOException {
UUID uuid = UUID.randomUUID();
Map map = new HashMap<>();
map.put(uuid,message);
try {
session.getBasicRemote().sendObject(map);
} catch (EncodeException e) {
e.printStackTrace();
}
}