无法使用socketcluster Java客户端

时间:2017-12-23 17:57:22

标签: java websocket stream socketcluster

我试图使用Coinigy websocket api来获取实时交易流。

但我收到了一个错误:

  

{" rid":2,"错误":"您已连接,但此套接字尚未   认证。请使用凭据有效负载发出auth事件。"}   {"数据" {" pingTimeout":20000" ID":" BltoRYWc7k1dYgeRAHm0"" isAuthenticated":假},"去掉":1}

我使用的代码如下

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();
    }
}

1 个答案:

答案 0 :(得分:0)

如果使用套接字群集库,则只能对此进行身份验证。否则,您需要使用更复杂且未记录的协议。这里有一小段文档:https://github.com/SocketCluster/socketcluster/blob/master/socketcluster-protocol.md