I'm making a simple chat function on android where a client can go into a group and send messages to that group. On the client side, I can see from logging that I am connected to the socket, but any communications don't seem to happen. On the other hand, in the client side, I don't receive any errors, and I see all the logs that I placed in the order I expect them to as well. I just can't figure out how the socket is connected and the codes seem to be fine but the client never communicates to the server.
Below is a code for my client side in a file called socketioHandler.js
exports.ioConnections = io => {
io.on('connection', function(socket){
console.log('user connected');
// join a group
socket.on('join group', function(object){
console.log("room: " + object.groupId);
socket.join(object.groupId);
io.local.emit('join group', "joined"); //send message to user
});
// send a message to the group
socket.on('send message', function(object){
console.log(' smessage: ' + object.message);
socket.broadcast.to(object.groupId).emit('send message', {message: object.message});
updateGroupConversation(object);
});
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
}
and below is a code from my fragment
private Socket mSocket;
{
Log.i(TAG, "creating socket");
try {
mSocket = IO.socket(Constants.API_URL);
} catch (URISyntaxException e) {
e.printStackTrace();
System.out.print("something happened\n");
}
Log.i(TAG, "created socket!");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mSocket.connect();
mSocket.on("join group", onJoined);
mSocket.on("send message", onMessageReceive);
attemptJoinRoom();
}
private Emitter.Listener onJoined = new Emitter.Listener() {
@Override
public void call(Object... args) {
Log.e("Response", "in joined");
}
};
private void attemptJoinRoom() {
JSONObject object = new JSONObject();
try {
object.put("groupId", getArguments().getString("groupID"));
} catch (JSONException e) {
e.printStackTrace();
}
mSocket.emit("join group", object);
Log.i(TAG,"joined group");
}
Edit: Log on the server side looks like this
2018-04-30T12:52:31.828761+00:00 app[web.1]: user connected
2018-04-30T12:52:32.133621+00:00 heroku[router]: at=info method=POST path="/socket.io/?EIO=3&transport=polling&sid=CVu2daOEA0uT1wBSAAAG" host=group-app-android.herokuapp.com request_id=157c6758-b655-4e10-ab43-9190bf41a1f5 fwd="91.230.41.206" dyno=web.1 connect=3ms service=4ms status=200 bytes=208 protocol=https
2018-04-30T12:52:32.145397+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&sid=CVu2daOEA0uT1wBSAAAG" host=group-app-android.herokuapp.com request_id=74c64b44-edc4-4938-9516-98031387cde4 fwd="91.230.41.206" dyno=web.1 connect=0ms service=3ms status=200 bytes=256 protocol=https
2018-04-30T12:52:34.534160+00:00 heroku[router]: at=info method=GET path="/socket.io/?EIO=3&transport=polling&sid=CVu2daOEA0uT1wBSAAAG" host=group-app-android.herokuapp.com request_id=dbc4f21a-38bb-430f-be4f-068279f4d80f fwd="91.230.41.206" dyno=web.1 connect=1ms service=2175ms status=200 bytes=225 protocol=https
Edit: mSocket.connected()
returns false right after mSocket.connect()
on the client side. It doesn't make sense because I do get a user connected on the server side.
The problem was modification made in build.gradle
.
I messed up one of the directories and I decided to start fresh and newly clone a repo again. During this process, as I was new to android, a modification in build.gradle
was made automatically by Android Studio when I clicked some buttons.
I reverted my build.gradle
to a previous state and the problem was solved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.