android java socket.io is not emitting any events

2019-08-02 18:42发布

问题:

I have a working nodejs server running, (and through a browser the socket.io works perfectly fine, emitting and receiving events). To easily simulate an online connection, im using ngrok.io to tunnel my localhost. This all works well. I wanted to write a small app that connects to the same server, but while my server is able to recieve the socket.connect() call, it doesnt recieve any .emit() events.
(im using com.github.nkzawa:socket.io-client:1.0.0 and socket.io 2.1.1)

here is the relevant server code:

    io.on('connection', function (socket) {
            console.log('Something has connected to the server: ' + socket.request.session.id);

        socket.on('join', function (username) {
                if(!username.username){
                    username = socket.request.session.username;
                }else{
                    username = username.username;}
            console.log('[Socket Chat] '+ username +' has connected to chat');
            socket.broadcast.emit('sys message', username + " has joined the chat.");

                });

    });  

Again, this code works well.

the other parts of the app connect to my expressjs server fine, and are able to connect to a database to auth a login and stuff. So theres no issues with the connection itself, just the socket.io portion of it.
here is the relevant app code (which i run on my phone):

 private Socket mSocket;
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat);
        try {
                //IO.Options opts = new IO.Options();
                //opts.port = 3000; tried changing post but didn't help

                mSocket = IO.socket(AppConfig.URL_CHAT); // URL_CHAT is correct, and its the same url I use with the browser
            }
        catch (URISyntaxException e) {
                throw new RuntimeException(e);
            }

        mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError); //theres an emitter.listener for these, and they do trigger if my url is incorrect or my internet is down
        mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);

        mSocket.connect();
        JSONObject obj = new JSONObject();
                try {
                    obj.put("username", Nickname);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //mSocket.open();
                mSocket.emit("join", obj); 

I have tried every method from every other answer on here to the best of my abilities. When I run the app, my server does print the log "Something has connected: $id" with a session ID, but the join event does not get triggered. in fact, I have socket-wildcard installed and it also doesn't pick up any emitted events, usingthe code:

socket.on('*', function (packet){
            console.log(packet.data);
        });

which does pick up all other events from the chat.

Ive tried sending different kinds of events, diffrent json data, strings. One thing I noticed is that in the app, socket.connected() comes back as false, which is weird cause my server does also detect when the app disconnects.
ive tried downgrading socket.io on node to 2.0.0, but that also doesnt work.

There are no errors that come out on either logs, so Im unable to fully understand whats wrong with this thing. how do i make my server catch the .emit() events?

回答1:

I figured it out, here's an answer for anyone who has a similar problem.
The socket server is on site.com/chat. when i did IO.socket(URL), the thing decided that the default namespace would be "/chat" which is not the case. this is why my .on() events werent triggering. if I added a io.of('/chat') into my server, those event would trigger.

so one solution would be to make the default namespace "/chat", but i went with something a bit different and added

Manager manager = new Manager(new URI("site.com/chat"));
mSocket = manager.socket("/"); 

so now my emit()s are working!