处理:标签文本通过网络事件更新缓慢

Processing: Label text slow to update via network events

我正在使用 processing.net 库开发一个从外部程序(特别是 OpenFrameworks 草图)接收网络事件的草图。

在 draw 方法中,我有以下代码来解析传入的数据,并适当地分配它以在文本标签中显示文本值:

void draw()
{
  // check for incoming data
  Client client = server.available();
  if (client != null) {
    // check for a full line of incoming data
    String line = client.readStringUntil('\n');

    if (line != null) {
      //println(line);
      int val = int(trim(line)); // extract the predicted class
      //println(val);
      if (val == 1) {
        messageText = "EVENT 1";
      } else if (val == 2) {
        messageText = "EVENT 2";
      } else if (val == 3) {
        messageText = "EVENT 3";
      }
    } 
  }

  // draw
  background(0);
  textFont(f,64);
  fill(255);
  textAlign(CENTER);
  text(messageText, width/2, height/2);

}

通过日志记录,我已验证数据正在正确接收

但是,我遇到了一个非常烦人的错误 - 我的 messageText 标签的文本更新非常慢...在发生新事件后(并通过日志记录显示),messageText 仍然会显示最后一个事件的值几秒钟。

有人对如何提高此处的性能有任何指示吗?

谢谢!

编辑:下面是完整的草图代码:

import processing.net.*; // include the networking library

Server server; // will receive predictions 
String messageText;
PFont f;

void setup()
{
  fullScreen();
  //size(200,200);

  server = new Server(this, 5204); // listen on port 5204
  messageText = "NO HAND";

  f = createFont("Arial",16,true); // Arial, 16 point, anti-aliasing on
}

void draw()
{
  // check for incoming data
  Client client = server.available();
  if (client != null) {
    // check for a full line of incoming data
    String line = client.readStringUntil('\n');

    if (line != null) {
      //println(line);
      int val = int(trim(line)); // extract the predicted class
      //println(val);
      if (val == 1) {
        messageText = "EVENT 1";
      } else if (val == 2) {
        messageText = "EVENT 2";
      } else if (val == 3) {
        messageText = "EVENT 3";
      }
    } 
  }

  // draw
  background(0);
  textFont(f,64);
  fill(255);
  textAlign(CENTER);
  text(messageText, width/2, height/2);

}

EDIT2 正如凯文在下面指出的那样,我的解决方案相当老套。我正在尝试使用 Networking 库中的 Message Events 方法,而不是将我所有的网络代码都塞入 draw() 方法中。

所以,我尝试这样实现 clientEvent 方法。但是,我想我可能误解了什么……尽管我原来的、hacky 代码现在似乎可以正常工作,但我下面使用此委托方法的新代码似乎根本不起作用。基本上,我必须先 运行 我的草图,它创建一个服务器,我的外部程序连接到该服务器。然后该程序发出我的处理草图接收到的数据。

这是我的完整草图 - 谁知道我的误解可能来自哪里?

import processing.net.*; // include the networking library

Server server; // will receive predictions 
Client client;
String messageText;
int dataIn;
PFont f;

void setup() {
  fullScreen(P3D);
  frameRate(600);
  server = new Server(this, 5204); // listen on port 5204
  client = server.available();

  messageText = "NO HAND";
  textAlign(CENTER);
  fill(255);
  f = createFont("Arial",48,true); // Arial, 16 point, anti-aliasing on
  textFont(f, 120);
}

void draw() {
  // draw
  background(0);
  text(messageText, width/2, height/2);
}

// If there is information available to read
// this event will be triggered
void clientEvent(Client client) {
  String msg = client.readStringUntil('\n');
  // The value of msg will be null until the 
  // end of the String is reached
  if (msg != null) {    
      int val = int(trim(line)); // extract the predicted class
      println(val);
      if (val == 1) {
        messageText = "A";
      } else if (val == 2) {
        messageText = "B";
      } else if (val == 3) {
        messageText = "C";
      } else if (val == 4) {
        messageText = "D";
      }
    }
  }

}

所以,答案最终与项目中使用的帧率和渲染器有关。由于在我的草图的 draw 方法中调用了网络更新代码,因此调用它的速度取决于所使用的 framerate/renderer。

经过一些试验/反复试验后,将草图更改为使用 FX2D 渲染器和 600 帧速率,显着提高了性能,达到了我需要的程度。

void setup()
{
  fullScreen(FX2D);
  frameRate(600);
  ...
}

编辑: 在与 Processing 核心团队成员之一交谈后,我认为我的网络代码正确且完整。将渲染器更改为 FX2D 显着提高了性能。

在我非常具体的用例中,我 运行 在配备 Retina 显示屏的 MacBookPro 上全屏草图。提高帧率值并更改渲染器,为我提供了快速原型草图所需的性能。