Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot authenticate wesocket connection from java client #23

Open
tyagi85prashant opened this issue Dec 25, 2017 · 7 comments
Open

cannot authenticate wesocket connection from java client #23

tyagi85prashant opened this issue Dec 25, 2017 · 7 comments

Comments

@tyagi85prashant
Copy link

Hi i am trying to get websocket stream using following code

`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", "eee");
    obj.put("apiSecret", "xxx");
    socket.setListener(new BasicListener() {

        public void onConnected(Socket socket,Map<String, List<String>> headers) {
            System.out.println("Connected to endpoint");    
            socket.emit("auth", obj.toString(), 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("RADE-OK--BTC--CNY");
    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();
}

}`

I am emitting the auth msg but still i ma getting the following error:

NFO: Message :{"rid":2,"error":"Your are connected but this socket has not been authenticated. Please emit auth event with credentials payload."}
Dec 26, 2017 3:27:28 AM io.github.sac.Socket$1 onFrame
INFO: Message :{"data":{"pingTimeout":20000,"id":"vXgm9NvKjCcvB6mVACFj","isAuthenticated":false},"rid":1}

Can you please help

@kyle-copeland
Copy link

I believe you need to set socket.setAuthToken before you run socket.connect(). Auth should happen automatically. You don't need to manually emit an auth event. Let me know if that works for you.

@sacOO7
Copy link
Owner

sacOO7 commented Jan 24, 2018

Yeah @kyle-copeland Thanks for the reply 👍

@dalakada
Copy link

dalakada commented Feb 1, 2018

can you please provide an sample working code, I'm also having the same issue

@aviolide
Copy link

please example of working code!

@sacOO7
Copy link
Owner

sacOO7 commented Mar 17, 2018

@dalakada @Desided @tyagi85prashant Please look here for solution #17

@sacOO7
Copy link
Owner

sacOO7 commented Aug 22, 2018

@sacOO7
Copy link
Owner

sacOO7 commented Aug 22, 2018

@Desided @dalakada @tyagi85prashant @kyle-copeland , please let me know your updates on this. Will close the issue, once it gets resolved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants