Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随着时间的推移,使用java websockets的模拟视频直播将变得无响应_Java_Javascript_Websocket_Glassfish - Fatal编程技术网

随着时间的推移,使用java websockets的模拟视频直播将变得无响应

随着时间的推移,使用java websockets的模拟视频直播将变得无响应,java,javascript,websocket,glassfish,Java,Javascript,Websocket,Glassfish,我们正在进行一项研究,使用JavaWebSockets将模拟图像实时发送到HTMLWeb客户机。我们已经注意到,在整个研究过程中,系统在¼处变得无反应,并发现如果我们移除 session.getBasicRemote.sendBinarybuf line除了模拟中没有视频外,其他一切都正常运行。然而,一旦我们将这一行添加回系统,系统最初会工作,但最终会减慢/变得无响应 我不明白为什么会发生这种情况,也不知道该怎么办。任何帮助都将不胜感激。下面我包含了一段Java代码,它将图像作为二进制文件抓取并

我们正在进行一项研究,使用JavaWebSockets将模拟图像实时发送到HTMLWeb客户机。我们已经注意到,在整个研究过程中,系统在¼处变得无反应,并发现如果我们移除

session.getBasicRemote.sendBinarybuf

line除了模拟中没有视频外,其他一切都正常运行。然而,一旦我们将这一行添加回系统,系统最初会工作,但最终会减慢/变得无响应

我不明白为什么会发生这种情况,也不知道该怎么办。任何帮助都将不胜感激。下面我包含了一段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;
}

所以我相信我已经回答了我自己的问题。我们将WebSocket从同步切换到异步,这似乎解决了Mac上的问题。由于某些原因,在Ubunutu上测试时,异步不起作用。我不清楚为什么从同步切换到异步可以解决这种情况

在代码中,我们切换了以下内容

谢谢

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);
    }
}
session.getBasicRemote().sendBinary(buf);
session.getAsyncRemote().sendBinary(buf);