Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (778)
Games in Android Showcase (231)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  trouble reading from a NodeJS URL  (Read 3454 times)
0 Members and 1 Guest are viewing this topic.
Offline philfrei
« Posted 2018-02-26 01:25:15 »

A JSON file of Motion Capture data is being broadcast from a URL powered by a NodeJS package called WS.
https://www.npmjs.com/package/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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
      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:

1  
2  
3  
4  
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.

music and music apps: http://adonax.com
Offline ra4king

JGO Kernel


Medals: 508
Projects: 3
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2018-02-26 06:42:38 »

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:
1  
2  
URL url = new URL("....");
InputStream stream = url.openStream();

Offline princec

« JGO Spiffy Duke »


Medals: 1061
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #2 - Posted 2018-02-26 09:25:56 »

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

Cas Smiley

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2018-02-26 19:28:16 »

Hilarious.

Thank you both. I won't get a chance to test it until next Sunday.  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.

music and music apps: http://adonax.com
Offline philfrei
« Reply #4 - Posted 2018-03-01 08:04:28 »

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.

music and music apps: http://adonax.com
Offline philfrei
« Reply #5 - Posted 2018-03-04 07:33:19 »

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.

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.

music and music apps: http://adonax.com
Offline KaiHH

JGO Kernel


Medals: 650



« Reply #6 - Posted 2018-03-05 08:33:08 »

Quote
Are people here making use of it?
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.
Offline princec

« JGO Spiffy Duke »


Medals: 1061
Projects: 3
Exp: 20 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2018-03-05 09:28:17 »

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 Smiley

Offline philfrei
« Reply #8 - Posted 2018-03-06 04:12:45 »

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.

music and music apps: http://adonax.com
Pages: [1]
  ignore  |  Print  
 
 

 
hadezbladez (358 views)
2018-11-16 13:46:03

hadezbladez (190 views)
2018-11-16 13:41:33

hadezbladez (361 views)
2018-11-16 13:35:35

hadezbladez (90 views)
2018-11-16 13:32:03

EgonOlsen (2190 views)
2018-06-10 19:43:48

EgonOlsen (2226 views)
2018-06-10 19:43:44

EgonOlsen (1384 views)
2018-06-10 19:43:20

DesertCoockie (2022 views)
2018-05-13 18:23:11

nelsongames (1674 views)
2018-04-24 18:15:36

nelsongames (2312 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!