How to use feeds in Java/Scala

Added by Henrik over 3 years ago

Not sure I understand what needs to be done to get the feeds running, login using REST is no problem but using TCP with RSS proves to be more problematic in Scala.

Below is a simple example using SSLSocket from Scala (however should be very similar to how Java looks since im using Java libs). When I run this code the while loop just runs forever, that is im getting no response from the server.

@ val requestData: String = s"""{"cmd":"login", "args":{ "session_key":"$sessionKey"}}${10.toChar}"""

var sslsocketfactory: SSLSocketFactory = SSLSocketFactory.getDefault().asInstanceOf[SSLSocketFactory]
var sslsocket: SSLSocket = sslsocketfactory.createSocket(host, port).asInstanceOf[SSLSocket]
sslsocket.setUseClientMode(true)
sslsocket.startHandshake()
var os = new PrintStream(new BufferedOutputStream(sslsocket.getOutputStream()))
var is = new BufferedInputStream(sslsocket.getInputStream())
os.println(requestData)
Thread.sleep(1000)
os.print(s"""{"cmd":"subscribe", "args":{"t":"price", "i":"1869", "m":30}}${10.toChar}""")
while(is.available()<1) {
log.info(sslsocket.isConnected.toString)
Thread.sleep(1000)
}
val buf = new Array[Byte](is.available)
is.read(buf)
val input = new String(buf)
log.info(input)
os.close()
is.close()
sslsocket.close()@

What I expect from this code is to get a Heatbeat message in input after 5 sec but nothing ever happens. What am I missing?


Replies (1)

RE: How to use feeds in Java/Scala - Added by David over 3 years ago

Late reply, but....

I would check locally so that you send what you expect. Not going in to the java/scala parts, just how I would check in any language.

1/ Just check the network traffic. Wireshark for example. Even if you dont decrypt the ssl-traffic you can see if you send or receive data.

2/ Start a local server to connect to, and manually reply with a message to see that the code is right, and it flushes and have correct newlines. (It might be that you end with 10.toChar, but send it with println, which will add atleast newline, and maybe \\r. And error might disconnect you or something)

An easy way to start a local server with ssl is to use ncat.  It is like netcat, but with ssl/support.  Can be downloaded here: http://nmap.org/ncat
For example run with: ncat -v --ssl -l 1234
That will listen on port 1234, with ssl, and give you nice debug messages. Connect with a browser, curl or whatever to https://127.0.0.1:1234 just to see the output. After that, set 127.0.0.1:1234 as your feed endpoint, to see what you send. You can also reply by pasting your reply-messages to the window. You can also log it to file with -x or -o for further inspection.

3/ If all failed so far, connect to nordnet with ncat. Log in via rest, craft your payload in a text editor, and then paste or pipe them to ncat. (If you run windows, I dont know about newlines when pasting though)

Hope this helps. Cheers

(1-1/1)