1
服務器代碼的Java字節緩衝區(的WebSocket)在Java中:如何發送/讀取JavaScript客戶端的onMessage功能
@OnMessage
public void onMessage(Session session, ByteBuffer message) {
if (session.isOpen()) {
String msg = new String(message.array());
System.out.println("Message from " + session.getId() + ": " + msg);
try {
session.getBasicRemote().sendBinary(ByteBuffer.wrap("I have got the message".getBytes()));
} catch (IOException ioe) {
System.out.println(ioe.toString());
}
} else {
System.out.println("Session is not open");
}
}
客戶端代碼中的Javascript:
webSocket = new WebSocket("ws://192.168.10.1:2525/myChat/chat");
webSocket.binaryType = 'arraybuffer';
webSocket.onopen = function(event) {
updateOutput("Connected!");
connectBtn.disabled = true;
sendBtn.disabled = false;
};
webSocket.onmessage = function(event) {
updateOutput(event.data);
};
注: 服務器的代碼工作正常時,我將它與Web GL客戶端一起使用,因爲它發送的是二進制數據。
@OnMessage
public void onMessage(Session session, String message) {}
謝謝建議的任何意見:當我看到在服務器端 (Java代碼)字符串數據 JavaScript客戶端工作正常。