我们正在进行一项研究,我们使用java Web套接字实时将模拟图像发送到HTML Web客户端。我们注意到系统在研究过程中没有响应,发现如果我们删除了
session.getBasicRemote()sendBinary(BUF)。
除了没有来自模拟的视频外,行一切正常。但是,一旦我们添加此行,系统最初会起作用,但最终会变慢/变得无法响应。
我不明白为什么会这样,也不知道该怎么办。任何帮助,将不胜感激。下面我已经包含了一段Java代码,它将图像作为二进制文件抓取并发送给客户端。目前在客户端,我们什么都不做二进制数据接受打印数据的大小。
//Grab a binary version of the image from the simulation. Occurs 10 fps
protected void schedST_PostStageScreenMessage() {
LOG.log(Level.FINEST, "schedST_PostStageScreenMessage()");
//if(getCurrentDisplayMode() == DisplayMode.FULL){
// get the current proxy state
ProxyState state = this._robot.getProxy().getCurrentState();
if (state == null) {
LOG.log(Level.WARNING, "schedST_PostStageScreenMessage() - no state");
return;
}
// get the latest received screen message from the sim proxy
ScreenMessage screenMsg = state.msgLastScreen();
if(screenMsg == null) {
LOG.log(Level.FINER, "schedST_PostStageScreenMessage() - no update");
return;
}
// check to see if it has been updated since last time
if(this._screenMsg == screenMsg) {
LOG.log(Level.FINEST, "schedST_PostStageScreenMessage() - no screen message update");
return;
}
this._screenMsg = screenMsg;
ScreenResponse sr = (ScreenResponse)createSceneMessage();
doPost(sr.getPngData());
//doPost(createSceneMessage());
//}
}
private void doPost(byte[] message){
try {
postMessage(message);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Caught: " + e, e);
}
}
protected void postMessage(byte[] message) {
WebSocketEndpointERA.sendToAll(message);
}
public static void sendToAll(final byte[] data){
ByteBuffer buf = ByteBuffer.wrap(data);
System.out.println("Sending bytes of length: " + data.length);
Set<Session> sessions = getSessions();
try{
for (Session session : sessions) {
//When this line is commented out the system works
//However when we send the binary image the system works initially
//and then becomes unresponsive
session.getBasicRemote().sendBinary(buf);
}
}catch(IOException io){
LOG.log(Level.SEVERE, "unable to send binary message: {0}", io);
}
buf = null;
}
在Javascript方面,我们暂时使用以下内容进行测试
function onMessage(event) {
//console.log('Received a message!');
if (event.data instanceof ArrayBuffer) {
//handleBinaryEventMessage(event.data);
//Do nothing with the binary data right now
return;
}
var msg = JSON.parse(event.data);
//writeToMessages("Received message: " + msg.type);
switch (msg.type) {
case 'RobotPoseResponse':
handleRobotPoseMessage(msg);
break;
case 'RobotAcousticResponse':
handleRobotSensorDataMessage(msg);
break;
case 'RobotGoalResponse':
handleRobotGoalMessage(msg);
break;
case 'AvailableQuestionsResponse':
handleOptionsEventMessage(msg);
break;
case 'ExplanationResponse':
handleExplanationMessage(msg);
break;
case 'ScenarioResponse':
handleScenarioMessage(msg);
break;
case 'ScreenResponse':
handleScreenMessage(msg);
break;
case 'SceneResponse':
handleSceneMessage(msg);
break;
case 'StageStartUpResponse':
handleStageStartUp(msg);
break;
default:
writeToEvents("Received unhandled message: " + msg.type);
}
}
答案 0 :(得分:0)
所以我相信我已经回答了我自己的问题。我们将Websockets从同步切换到异步,这似乎解决了我们Mac上的问题。出于某种原因,在Ubunutu上进行测试时异步不起作用。我不清楚为什么从同步切换到异步会解决这种情况。
在代码中我们切换了以下内容
来自
session.getBasicRemote().sendBinary(buf);
到
session.getAsyncRemote().sendBinary(buf);
谢谢