ripple-unmaintained / ripple-lib-java

Java version of ripple-lib (work in progress)
ISC License
126 stars 109 forks source link

Getting Account Subscribe Stream ? #80

Open inmyth opened 7 years ago

inmyth commented 7 years ago

Is there a way (using this library or any other) to get a stream from subscribe command similar to Ripple's websocket tool. ? With any websocket client I can only get the initial response but not the stream itself.

inmyth commented 7 years ago

I think I'm getting a clue here. This library is using TooTallNate websocket. It looks with this client subscribe to account never returns anything. However subscribe to anything else works. This is the snippet. To test it, run the program and send some order with your account.

import java.net.URI;
import java.net.URISyntaxException;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;

public class Main extends WebSocketClient {
    static String SERVERLEDGER = "{\r\n  \"command\": \"subscribe\",\r\n  \"streams\": [\r\n    \"server\",\r\n    \"ledger\"\r\n  ]\r\n}";
    static String ACCOUNT = "{\r\n  \"command\": \"subscribe\",\r\n  \"accounts\": [\"raNDu1gNyZ5hipBTKxm5zx7NovA1rNnNRf\"]\r\n}";
    static String ORDERBOOK= "{\r\n    \"command\": \"subscribe\",\r\n    \"books\": [\r\n        {\r\n            \"taker_pays\": {\r\n                \"currency\": \"XRP\"\r\n            },\r\n            \"taker_gets\": {\r\n                \"currency\": \"JPY\",\r\n                \"issuer\": \"rB3gZey7VWHYRqJHLoHDEJXJ2pEPNieKiS\"\r\n            },\r\n            \"snapshot\": true\r\n        }\r\n    ]\r\n}";

    static Main main ;
    public Main(URI serverUri, Draft protocolDraft) {
        super(serverUri, protocolDraft);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onClose(int arg0, String arg1, boolean arg2) {
        System.out.println("disconnected " + arg1);
    }

    @Override
    public void onError(Exception e) {
        System.out.println(e.getMessage()); 
    }

    @Override
    public void onMessage(String raw) {
        System.out.println(raw);
    }

    @Override
    public void onOpen(ServerHandshake arg0) {
        System.out.println("connected");
        main.send(ACCOUNT); 
    }

    public static void main(String[] args) throws URISyntaxException {
        main = new Main(new URI("wss://s1.ripple.com"), new Draft_6455());
        main.connect();
    }

}

Later I'm testing this with node and ws websocket client and it can subscribe to account just fine.

const WebSocket = require('ws')

const ws = new WebSocket('wss://s1.ripple.com' )

const subscribeTx = '{\r\n  \"command\": \"subscribe\",\r\n  \"accounts\": [\"raNDu1gNyZ5hipBTKxm5zx7NovA1rNnNRf\"]\r\n}'

ws.on('open', function open() {
  console.log('connected')
  ws.send(subscribeTx)
})

ws.on('message', function incoming(data) {
  console.log(data)
})