随着时间的推移,使用 java websockets 实时流式传输模拟视频变得无响应

Posted

技术标签:

【中文标题】随着时间的推移,使用 java websockets 实时流式传输模拟视频变得无响应【英文标题】:Live streaming of simulation video using java websockets becomes unresponsive over time 【发布时间】:2015-03-07 18:28:02 【问题描述】:

我们正在进行一项研究,其中我们使用 Java Web 套接字将图像从模拟实时发送到 html Web 客户端。我们注意到系统在研究的 1/4 过程中变得无响应,并发现如果我们移除

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);
    

【问题讨论】:

【参考方案1】:

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

在代码中我们切换如下

来自

session.getBasicRemote().sendBinary(buf);

session.getAsyncRemote().sendBinary(buf);

谢谢

【讨论】:

以上是关于随着时间的推移,使用 java websockets 实时流式传输模拟视频变得无响应的主要内容,如果未能解决你的问题,请参考以下文章

JavaFX 滚动表更新性能随着时间的推移而降低

如何使用ggplot2随着时间的推移绘制p值?

随着时间的推移旋转游戏对象

随着时间的推移旋转游戏对象

swift spritekit 随着游戏时间的推移增加重力?

为啥我的应用程序随着时间的推移变得反应迟钝?