Closed Hayk985 closed 6 years ago
Hi @Hayko985 , little busy those days. Will look into the issue :+1:
And I don't think you will need to create a thread to connect to server. You can just use connectAsync() Method. It will try to connect to server in background thread.
For the first time, you need to set up listener before connecting to server. Otherwise it won't work. You can check example here https://github.com/sacOO7/socketcluster-client-java/blob/master/src/main/java/Main.java
I believe your code should be more like this
sc = new Socket(url);`
sc.setListener(new BasicListener() {
public void onConnected(Socket socket, Map<String, List<String>> headers) {
Log.d(TAG, "Connected to endpoint");
JSONObject authObject = new JSONObject();
try {
Log.d(TAG, "authObject");
authObject.put("accessToken", access_token;
authObject.put("refreshToken", refresh_token);
sc.emit("auth", authObject, new Ack() {
@Override
public void call(String name, Object error, Object data) {
Log.d(TAG, "Ack");
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onDisconnected(Socket socket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) {
Log.d(TAG, "Disconnected from end-point");
}
public void onConnectError(Socket socket, WebSocketException exception) {
Log.d(TAG, "Got connect error " + exception);
}
public void onSetAuthToken(String token, Socket socket) {
Log.d(TAG, "Set auth token got called. Here is your token: " + token);
}
public void onAuthentication(Socket socket, Boolean status) {
if (status) {
Log.d(TAG, "socket is authenticated");
} else {
Log.d(TAG, "Authentication is required (optional)");
}
> //Here I'm create channel with my username, then I'm subscribing to this channel.
final Socket.Channel channel = sc.createChannel(myUserName);
channel.subscribe();
> //Here I'm listening to this channel
channel.onMessage(new Emitter.Listener() {
@Override
public void call(String name, final Object data) {
Log.d(TAG, "Got data from server: " + data.toString());
JSONObject object = new JSONObject(data.toString());
final String message = object.getString("message");
sender = object.getString("from");
messagesAdapter.addToStart(MessagesFixtures.getTextMessage(message + " " + sender, "1"), true);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
sc.setReconnection(new ReconnectStrategy().setDelay(2000).setMaxAttempts(10));
sc.connectAsync();
messageInput.setInputListener(this);
Simple and elegant :)
Please let me know if this works for you :+1:
@sacOO7 Thank you, I'll test it later. And one more question too. Can I move socket channel creating part out of onAuthentication method in basic listener. And what you can suggest me to add in this code or what to remove from here? Thank you one more time))
@sacOO7 And one more question too.
sc.emit("auth", authObject, new Ack() { @Override public void call(String name, Object error, Object data) { Log.d(TAG, "Ack"); } });
In my "auth" event in onConnected method on basicListener sometimes not works. The server gets my tokens successfully, but I'm not getting acknowledgment. I add there a log Log.d(TAG, "Ack");
, but when I'm not getting acknowledgment, it don't displays the log. So what can be the problem? Is it necessuary
Well as long as you dont have any use for ack. I believe you can just leave it be. And about channel, you can move it somewhere else.
One more question ... Do you have any server method that listens to ack emit call?
@sacOO7 yeah, it works good but sometimes the emit don't work. So it jumps over the emit and not does the acknowledgment part. Is in my code all right? Any suggest why could happen a problem like this?
I'll move the channel part from there. If I understand, I send my tokens with emit and in success the program goes and does the acknowledgment part yes? Else it jumps over it and not executes the acknowledgment part. If the program not goes into acknowledgment that means that I'm doing something wrong with sending. And is there any time interval for sending? Example would be happen something if I'll send my tokens with emit many times?
Hey, don't try to send emit multiple times. There should be permanent solution to this :) for sure..
I believe server auth gets called everytime you send emit right? So, onAuthentication callback should get called with token and bool falg set to true, everytime you get authenticated.
It will be helpful if you can put breakpoint in there. Also provide server side code snippet that handles authentication in here (if you can).
@sacOO7 So I can't to send emit multiple times yes? In my example I'm sending 1 times in 3-4 seconds and that works almost 10-20 times and then 1 time it don't works. After that works again 10-20 times and fails and so on. And can you describe more what you mean saying bool flag set to true. Where is that bool flag? Sorry but server side code is not mine I can't) I have no their source code.
Which server API are you trying to use ? It might be server side issue.. if you are not getting callback for auth event. Or might be limit on number of auth requests you can send per minute. If you can tell me api about server, I will try to reproduce it on my local.
@sacOO7 I'll talk with them. If they let me, I'll can send you the api for testing.
Hi @Hayko985 any updates on this ?
@sacOO7 I think we can close this question. If there would be something new I let you know) Thank you.
@sacOO7 Hy, how are you? Can you help me I have one problem. I have a chat application where I'm registering , authorizing, connecting to socket. In basic listener: in onConnected method I'm sending my tokens into server. In onAuthenticated method I'm creating channel, then subscribing it. All works good, but when I'm creating a new account, the basic listener not works. So I'm connecting and then nothing. If I will logout and login again that will work.
So here's the code, I think you understood. If you not, I can explain more and more))
sender = bundle.getString(BUNDLE_USER_NAME); myUserName = SharedPreferencesManager.getInstance().getUserData().getUserName();
I'm creating channel with my userName which is unique. I need other user userName too. I'm getting them successfully.
Here is the code. When I'm creating new user and want to chat, the basic listener not works (so it jumps over it in debug mode). After logout and login it works successfull. So is in my code all right, am I done something wrong? Please help @sacOO7 . Thank you.