trouble reading from a NodeJS URL

A JSON file of Motion Capture data is being broadcast from a URL powered by a NodeJS package called WS.

I’ve been given the URL and Port. I’m able to connect a Socket: socket.isConnected() returns true.

When I try to create an InputStream and an InputStreamReader, the isReady() function of InputStreamReader returns false. Attempts to read a single int hang.

I also tried DataInputStream just to see if there was anything and available() function of DataInputStream returns false. Attempts to read a single int hang.

I’m new enough at this that there could be something really basic that I am missing. Any thoughts? Also, I am at a loss as to how to proceed to troubleshoot this.


		Socket socket = new Socket();
		InetSocketAddress socketAddr = new InetSocketAddress("192.168.1.189", 9100);
		
		socket.connect(socketAddr, 10_000);
		System.out.println("socked connected:" + socket.isConnected());
		System.out.println("             URL:" + socket.getInetAddress());
		System.out.println("            port:" + socket.getPort());
		
		InputStream inRT = socket.getInputStream();
//		InputStreamReader isrRT = new InputStreamReader(inRT, "UTF-8");
//		System.out.println("ISReader is ready:" + isrRT.ready());
//		int n = isrRT.read();
		
		
		DataInputStream disRT = new DataInputStream(inRT);
		System.out.println("DIS available:" + disRT.available());
		int n = disRT.readInt();
		System.out.println("n:" + n);
		
		
		socket.close();

Output:


socked connected:true
             URL:/192.168.1.189
            port:9100
DIS available:0

An elaborate work-around might be possible. It would involve tapping into the broadcast from the application that reads the MoCap data (Axis-Neuron). I can read data from this application if the motion data is sent out as text, but another user needs the data broadcast as binary, and only one broadcast is available. To use the binary, I would have to find and code from a spec–which maybe will be necessary if I can’t connect to the location streaming the JSON data.

Does it load when you visit the URL:port in a browser?

Also I recommend you use the URL class, instead of directly making the Socket:


URL url = new URL("....");
InputStream stream = url.openStream();

I suspect it’s waiting for you to send an HTTP GET first.

Cas :slight_smile:

Hilarious.

Thank you both. I won’t get a chance to test it until next Sunday. :stuck_out_tongue: Wishing I had thought of the obvious yesterday while there with the programmer who put the rig together and gave me the URL.

The Axis-Neuron application that connects to the motion capture suit broadcasts data continuously, and I made the assumption that downstream would continue in the same manner. But it makes perfect sense that if the Socket registers as connected but doesn’t supply any data, that it would need a call.

I did manage to get a parser library for BVH data to work last week. And, successfully ran a simple test where X-position controls a theremin pitch. Getting the live data has been the missing link.

It is starting to look like the issue is that the WS package makes a WebSocket, and a plain Java Socket isn’t going to work. I did find an Oracle tutorial on WebSockets in the Java EE realm, but I have to weigh figuring that out added to the additional task of parsing whatever form the data has been transformed into by the time it reaches that point, versus, reading the binary BVH stream into text form a couple of stages earlier in the chain.

I must say I’m very tempted to attempt the latter. I don’t fully understand the spec, but I do have the C++ code that performs the read from the binary stream. (Identifying the exact spot is tricky.) Most of a given record is 6 floats per 50-something bones (can’t recall exact fig. while typing this). There is also a couple header fields and there must be some sort of demarcation for a new record. I guess the issue with binary is to get your starting point correctly aligned? From there, one can do a known series of getFloat() or whatever?

The things we put ourselves through for art.

UPDATE: The code which reads the binary BVH file calls a .dll.

OK! Back to making progress.

This tutorial: Creating Binary WebSocket Connections with Java EE 7 and JavaFX, is excellent. In it, you build both a Server and a Client that make use of the WebSocket API (JS 356).

[quote]The WebSocket protocol, which was developed as part of HTML5, enables full-duplex and real-time communication to a Web server. The server and client can send and receive data at any time by using a single connection. WebSocket connections are started by using an HTTP request that enables the connection to change to the WebSocket protocol. These connections take advantage of all features of the HTTP protocol. Switching the connection allows for little overhead communication because only one HTTP handshake is performed for all exchanged messages.
[/quote]
Important “plus,” the tutorial works! I had a frustrating time with one I tried before this, which uses an HTML/CSS/JS client: Java EE 7: Building Web Applications with WebSocket, JavaScript and HTML5 For whatever reason, I could not get the Server to respond. It just lies dead in the water. The JS call to the Server executes (verified with console.log()), but none of the System.out.println()'s I put in the Java Server code print out. Whatever. For my needs, I don’t care so much about debugging the Server. It’s Java Clients that I want to be able to understand and code.

You don’t need Java EE to make a Client. The Client only really requires the library/projects files from Project Tyrus, and as far as I can tell, you aren’t required use JavaFX. Swing should also work for the GUI. In any event, a link in the tutorial will get you to a zip with the required files for the Client.

This seems like a really excellent resource for game networking! Are people here making use of it? (This is my first foray into Networking. I am pretty “talented” at putting on blinders and not noticing or listening to what others are doing.)

I’m noting that the WebSocket address makes use of a string as part of the URI. I was confounded by the presence of a String being given as part of a web address. I thought a web address should only have a URL with a Port. I don’t seen anything in the “Java Network Programming, 4th Ed.” PDF I downloaded about WebSockets API. I guess that goes to show the danger of free PDF’s being obsolete (part of why they are free). It was published in 2014.

[quote]Are people here making use of it?
[/quote]
I don’t think so. WebSockets are only a thing when you need to use HTTP, which is pretty much equivalent to: Your client is a HTML5/JavaScript application.

For everything else just use plain TCP or UDP sockets. Since you seem to be using a Node backend and a Java frontend, you can perfectly just use simple UDP or TCP sockets. They’ll both be simpler to implement/integrate (less library, less configuration) and will be faster (better performance) because the WebSocket frame algorithm (i.e. WebSocket is a packet-based protocol) is not needed in most cases, especially when you decide to use a transport protocol which itself is packet-based, like UDP.

You might also find that a lot of people are in environments that completely block any traffic other than HTTP/HTTPS. I think this is probably just misguided IT policy from someone who maybe doesn’t really understand the internet but I’ve seen it in a lot of places.

Cas :slight_smile:

I have to make use of the data flow path that already exists, tap in where I can. Since I can now use WebSocket to read from a C++ program called PNTransformer, I don’t have to worry about signal transformations or servers downstream from that point.

@KaiHH, can a plain Java Socket class be used connect and read from a JS 356 type WebSocket? I tried a bunch of ways to connect to PNTransformer, which uses the URI “localhost:9000/service” but couldn’t figure out any way to connect or get information from there with Socket. However, I am able to read from it with a WebSocket client.

@princec – Yes, I read about the tendency to block custom networking in the “Java Network Programming” PDF. Since WebSocket is a form of HTTP, though, it probably isn’t blocked? Or am I misunderstanding?

It seems (so far) like the Tyrus library is not a burden. There were seven or eight jar files to add to the project which doesn’t feel like a lot, and the basic commands (@OnOpen, @OnMessage) have been simple enough. I have to scramble to get a couple tools built for the next rehearsal–am looking forward to exploring this further later in the week.