0

I'm trying to use the Coinigy websocket api, to get the stream of real time trades.

But I am getting a error:

{"rid":2,"error":"Your are connected but this socket has not been authenticated. Please emit auth event with credentials payload."} {"data":{"pingTimeout":20000,"id":"BltoRYWc7k1dYgeRAHm0","isAuthenticated":false},"rid":1}

The code I am using is as follows

import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFrame;
import io.github.sac.*;

import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;


public class Main {

    public static String url="wss://sc-02.coinigy.com/socketcluster/";

    public static void main(String arg[]) throws JSONException {

        Socket socket = new Socket(url);
        JSONObject obj = new JSONObject();
        obj.put("apiKey", "abd");
        obj.put("apiSecret", "efg");
        socket.setListener(new BasicListener() {

            public void onConnected(Socket socket,Map<String, List<String>> headers) {
                System.out.println("Connected to endpoint");
                socket.emit("auth", obj, new Ack() {
                    @Override
                    public void call(String eventName, Object error, Object data) {
                        System.out.println("Got message for :"+eventName+" error is :"+error+" data is :"+data);
                    }
                });
            }

            public void onDisconnected(Socket socket,WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) {
                System.out.println("Disconnected from end-point");
            }

            public void onConnectError(Socket socket,WebSocketException exception) {
                System.out.println("Got connect error "+ exception);
            }

            public void onSetAuthToken(String token, Socket socket) {
                System.out.println("Set auth token got called");
                socket.setAuthToken(token);
            }

            public void onAuthentication(Socket socket,Boolean status) {
                if (status) {
                    System.out.println("socket is authenticated");
                } else {
                    System.out.println("Authentication is required (optional)");
                }
            }

        });


       socket.setReconnection(new ReconnectStrategy().setDelay(3000).setMaxAttempts(10)); //Connect after each 2 seconds for 30 times

        socket.connect();

        Socket.Channel channel = socket.createChannel("xyz");
        channel.subscribe(new Ack() {
            @Override
            public void call(String channelName, Object error, Object data) {
                if (error==null){
                    System.out.println("Subscribed to channel "+channelName+" successfully");
                }
            }
        });

        channel.onMessage(new Emitter.Listener() {
            @Override
            public void call(String channelName, Object data) {

                System.out.println("Got message for channel "+channelName+" data is "+data);
            }
        });

        channel.unsubscribe(new Ack() {
            @Override
            public void call(String name, Object error, Object data) {
                System.out.println("Unsubscribed successfully");
            }
        });
        channel.unsubscribe();
    }
}
tyagi
  • 265
  • 2
  • 10
  • In response to which event do you get the `{"rid":2,"error":"Your are connected but this socket has not been authenticated. Please emit auth event with credentials payload."}` error? – Jon Dec 30 '17 at 20:37

1 Answers1

0

You can only authenticate like that if you use the socket cluster library. Otherwise you need to use their more complex and rather undocumented protocol. There's a snippet of documentation here: https://github.com/SocketCluster/socketcluster/blob/master/socketcluster-protocol.md

Jakob Mygind
  • 134
  • 4
  • because the code is actually using the socketcluster library. – HyderA Feb 13 '18 at 21:27
  • The title was missing that little nugget of information when I answered, and as such the answer was fine. – Jakob Mygind Feb 27 '18 at 14:35
  • the code clearly shows the socketcluster library being used. – HyderA Feb 27 '18 at 22:24
  • Not really. It shows the url of the socketCluster API being used, which can be used without the library. There's no obvious imports of any library called SocketCluster. Which is why I assumed you wanted to use the api without the library, since the question didn't mention using the library. My answer as such didn't deserve a downvote. – Jakob Mygind Mar 03 '18 at 19:24
  • `import io.github.sac.*;` imports the socketcluster library. It's not my question. I downvoted because I didn't want future viewers of your answer to be misled. Nothing personal. – HyderA Mar 03 '18 at 22:04