Hi. I have a chat application where I'm using sockets. In this application I'm connecting to socket then I send my tokens into server then I'm creating channel -> subscribing -> listening to this channel with Channel.on . So the problem is that I'm can't to get messages with channel.on sometimes. Example when I'm login in this app I have to get message from server that right now only I'm online in this chat. When I do login, sometimes I get the message from server, but sometimes it don't comes. The backend for web works good, so I think I'm not done something in android. But how can I one time get the message and the second time not get?
Here's the sample code.
private void socketCall() {
sc = new Socket(url);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
sc.connect();
}
});
thread.start();
final Socket.Channel channel = sc.createChannel(myUserName);
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 {
authObject.put("accessToken", getAccessToken());
authObject.put("refreshToken", getRefreshToken());
sc.emit("auth", authObject);
channel.subscribe();
} catch (JSONException e) {
e.printStackTrace();
}
channel.onMessage(new Emitter.Listener() {
@Override
public void call(String name, final Object data) {
Log.d(TAG, "Got data from server: " + data.toString());
try {
JSONObject object = new JSONObject(data.toString());
String message = object.getString("message");
String sender = object.getString("from");
Log.d(TAG, "That's your message: " + message);
Log.d(TAG, "You got message from user: " + sender);
} 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)");
}
}
});
messageInput.setInputListener(this);
}
@Override
public boolean onSubmit(CharSequence input) {
if(sc.isconnected() && sc.getChannelByName(myUserName) != null) {
JSONObject messageObject = new JSONObject();
try {
messageObject.put("msg", input.toString());
messageObject.put("user", messageReceiverUserName);
sc.emit("messaging", messageObject);
} catch (JSONException e) {
e.printStackTrace();
return false;
}
sc.getChannelByName(myUserName).publish(input.toString(), new Ack() {
@Override
public void call(String name, Object error, Object data) {
Log.d(TAG, "Published message to channel: " + name + " successfully");
}
});
} else {
Toast.makeText(context, "Something went wrong", Toast.LENGTH_SHORT).show();
}
return true;
}
Here's the socket part. I'm creating channel with my username. So where I done something wrong?
Hi. I have a chat application where I'm using sockets. In this application I'm connecting to socket then I send my tokens into server then I'm creating channel -> subscribing -> listening to this channel with Channel.on . So the problem is that I'm can't to get messages with channel.on sometimes. Example when I'm login in this app I have to get message from server that right now only I'm online in this chat. When I do login, sometimes I get the message from server, but sometimes it don't comes. The backend for web works good, so I think I'm not done something in android. But how can I one time get the message and the second time not get?
Here's the sample code.
private void socketCall() {
Here's the socket part. I'm creating channel with my username. So where I done something wrong?
@sacOO7 Please help.