首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用OscP5监听tcp更改?

如何使用OscP5监听tcp更改?
EN

Stack Overflow用户
提问于 2013-07-07 00:54:59
回答 1查看 876关注 0票数 2

我在TCP模式下使用OscP5,但我似乎不知道如何监听TcpClient连接或断开连接等更改。

我像这样实例化oscP5:

代码语言:javascript
复制
tcp = new OscP5(this, TCP_PORT, OscP5.TCP);

但是,任何与TcpServer侦听器相关的内容都会导致空引用错误,例如

代码语言:javascript
复制
println(tcp.tcpServer().getListeners());

在此期间,我考虑过的一个棘手的解决办法是不断计算连接的数量:

代码语言:javascript
复制
tcp.tcpServer().size()

如果数量增加,则客户端已连接,如果数量减少,则客户端断开连接。问题是,当客户端断开连接时,我想不出一种方法来知道哪个ip/客户端id断开了,除非我有自己的列表,并检查哪个客户端在我自己的客户端列表中缺失。这感觉非常老套,而且很容易出错:

代码语言:javascript
复制
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.swing.JOptionPane;

import netP5.NetListener;
import netP5.NetMessage;
import netP5.NetStatus;
import netP5.TcpClient;
import oscP5.OscEventListener;
import oscP5.OscMessage;
import oscP5.OscP5;
import oscP5.OscStatus;
import processing.core.PApplet;


public class TCPServer extends PApplet implements NetListener,OscEventListener {

    public static final int TCP_PORT = 32002;

    private OscP5 tcp;
    private int maxClients = 100;
    private TcpClient[] clients = new TcpClient[maxClients];
    private HashMap<String,Integer> clientsMap = new HashMap<String, Integer>();//lookup table to check against duplicates
    private int prevSize = 0;//previous tcp server client list size

    public void setup() {
        size(1000,100);
        tcp = new OscP5(this, TCP_PORT, OscP5.TCP);
        tcp.addListener(this);
    }

    public void draw() {
        int currSize = tcp.tcpServer().size();//get updated size of clients list
        //if there was a change, a client either connected or disconnected
        if(currSize > prevSize) connect(tcp.tcpServer().getClient(prevSize));//prevSize = size-1 at this point
        if(currSize < prevSize) disconnect(); 
        prevSize = currSize;//update internal client size list

        background(255);
        for (int i = 0; i < maxClients; i++) {
            fill(clients[i] == null ? 255 : 127);
            rect(i*10,0,10,10);
        }
    }

    public void oscEvent(OscMessage m) {
        println(m);
    }

    private void connect(TcpClient client){
        String ip = client.netAddress().address();
        if(clientsMap.get(ip) == null){//if the ip is not a duplicate
            int id = getNextAvailableSlot();
            clients[id] = client;//keep track of this client
            clientsMap.put(ip, id);//and it's ip in the LUT
            println("connected " + ip + " at index " + id);
        }
    }
    private void disconnect(){
        List<TcpClient> c = Arrays.asList(tcp.tcpServer().getClients());//not very efficient, just lazy piggyback ride for contains
        for(int i = 0; i < maxClients; i++){//go through locally stored clients
            if(clients[i] != null){//if we one
                if(!c.contains(clients[i])) {//check if it's not already on the updated tcp server client list
                    String ip = clients[i].netAddress().address();
                    clientsMap.remove(ip);//clear LUT entry
                    tcp.tcpServer().remove(clients[i]);//disconnect
                    clients[i].dispose();//close
                    clients[i] = null;//request cleanup
                    println("disconnected " + ip);
                    return;
                }
            }
        }
    }
    private int getNextAvailableSlot(){
        for (int i = 0; i < maxClients; i++)
            if(clients[i] == null) return i;
        return -1;
    }

    public static void main(String[] args) {
        PApplet.main(TCPServer.class.getSimpleName());
    }

    @Override
    public void netEvent(NetMessage e) {
        System.out.println("netEvent:");
        System.out.println(e);
    }

    @Override
    public void netStatus(NetStatus s) {
        System.out.println("netStatus:");
        System.out.println(s);
    }

    @Override
    public void oscStatus(OscStatus s) {
        System.out.println("oscStatus:");
        System.out.println(s);
    }

}

在这种情况下,监控tcp客户端连接/断开连接事件的干净方法是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-07 16:47:31

目前在oscP5中还没有实现这样的概念。下面是一个类似的discussion

似乎你必须根据在调试信息中找到的内存地址来构建一些东西,例如### new Client @ netP5.TcpClient@164b9b6。我在System.out流中扫描字符串new Client,然后调用clientConnected方法。这完全是老生常谈,但我目前看不到其他方法。

代码语言:javascript
复制
import java.io.PrintStream;

import oscP5.*;
import netP5.*;

OscP5 oscP5tcpServer;
OscP5 oscP5tcpClient;
NetAddress myServerAddress;

void setup() {
  // overwrite standard System.out
  System.setOut(new PrintStream(System.out) {
    public void println(String s) {
      if(s.indexOf("new Client") != -1){
        clientConnected(getMemoryAddress(s));
      }
      super.println(s);
    }
    // override some other methods?
  });

  oscP5tcpServer = new OscP5(this, 11000, OscP5.TCP);
  oscP5tcpClient = new OscP5(this, "127.0.0.1", 11000, OscP5.TCP);
}

void clientConnected(int memoryAddress){
  println("Client connected, with memory address " + memoryAddress);
}

int getMemoryAddress(String s){
  return Integer.parseInt(s.substring(s.lastIndexOf('@')+1), 16);
}

void draw() {  
}

void mousePressed() {
  oscP5tcpClient.send("/test", new Object[] {new Integer(1)});
}

void keyPressed() {
  println(oscP5tcpServer.tcpServer().getClients().length);
}

void oscEvent(OscMessage theMessage) {
  System.out.println("### got a message " + theMessage);
  if(theMessage.checkAddrPattern("/test")) {
    OscMessage m = new OscMessage("/response");
    m.add("server response: got it");
    oscP5tcpServer.send(m,theMessage.tcpConnection());
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17505058

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档