处理:通过网络事件

时间:2017-07-23 00:04:59

标签: java networking processing

我正在使用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 正如凯文在下面指出的那样,我的解决方案相当苛刻。我正在尝试使用网络库中的消息事件方法,而不是将所有网络代码填充到draw()方法中。

所以,我尝试实现了clientEvent方法。但是,我想我可能会误解某些东西......即使我原来的hacky代码现在看起来工作正常,但我在下面使用这种委托方法的新代码似乎根本不起作用。基本上,我必须首先运行我的草图,它创建一个服务器,我的外部程序连接到。该程序然后发送我的Processing sketch收到的数据。

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

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";
      }
    }
  }

}

1 个答案:

答案 0 :(得分:0)

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

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

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

修改: 在与其中一位Processing核心团队成员交谈后,我正在考虑我的网络代码是否正确和完整。将渲染器更改为FX2D可显着提高性能。

在我的特定用例中,我在带有Retina显示屏的MacBookPro上全屏运行草图。将帧速率值提高并更改渲染器,为我提供了快速原型草图所需的性能。