简体   繁体   English

socket.io客户端连接到服务器,但无法通信

[英]socket.io client connects to the server but does not communicate

I'm making a simple chat function on android where a client can go into a group and send messages to that group. 我正在android上做一个简单的聊天功能,客户端可以进入一个组并将消息发送到该组。 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 下面是一个名为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. 编辑: mSocket.connected()在客户端的mSocket.connect()之后mSocket.connect()返回false。 It doesn't make sense because I do get a user connected on the server side. 这没有任何意义,因为我确实在服务器端连接了用户。

The problem was modification made in build.gradle . 问题是在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. 在此过程中,因为我是android的build.gradle ,所以当我单击一些按钮时,Android Studio会自动对build.gradle进行修改。

I reverted my build.gradle to a previous state and the problem was solved. 我将build.gradle恢复为以前的状态,问题已解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM