Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to listen for tcp changes using OscP5?
    text
    copied!<p>I'm using <a href="http://www.sojamo.de/libraries/oscP5/" rel="nofollow">OscP5</a> in TCP mode, but I can't seem to figure out how to listen for changes like a TcpClient connecting or disconnecting.</p> <p>I instantiate oscP5 like so:</p> <pre><code>tcp = new OscP5(this, TCP_PORT, OscP5.TCP); </code></pre> <p>but anything related to TcpServer listeners causes a null reference error, for example</p> <pre><code>println(tcp.tcpServer().getListeners()); </code></pre> <p>A hacky workaround I've thought about in the meantime is to constantly count the number of connections:</p> <pre><code>tcp.tcpServer().size() </code></pre> <p>If the number increases, a client connected, if the number decreases, the client disconnected. The problem is, when a client disconnects, I can't think of a way to know which ip/client id that disconnected, unless I have my own list and check which client is missing from my own list of clients. This feels very hacky and possibly error prone:</p> <pre><code>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&lt;String,Integer&gt; clientsMap = new HashMap&lt;String, Integer&gt;();//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 &gt; prevSize) connect(tcp.tcpServer().getClient(prevSize));//prevSize = size-1 at this point if(currSize &lt; prevSize) disconnect(); prevSize = currSize;//update internal client size list background(255); for (int i = 0; i &lt; 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&lt;TcpClient&gt; c = Arrays.asList(tcp.tcpServer().getClients());//not very efficient, just lazy piggyback ride for contains for(int i = 0; i &lt; 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 &lt; 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); } } </code></pre> <p>What is the clean way to monitor tcp client connect/disconnect events in this scenario ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload