nkzawa / socket.io-android-chat

A simple chat demo for socket.io and Android
MIT License
2k stars 691 forks source link

Problem with connecting to NodeJS server #36

Open joshterrill opened 8 years ago

joshterrill commented 8 years ago

For my NodeJS server code, I'm using the chat server code that is linked to in the socket io android tutorial.

I'm following the tutorial above to a tee in my android project.

build.gradle:

...
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.github.nkzawa:socket.io-client:0.3.0'
}

AndroidManifest.xml:

...
<uses-permission android:name="android.permission.INTERNET" />
...

MainActivity.java:

import com.github.nkzawa.socketio.client.IO;
import com.github.nkzawa.socketio.client.Socket;

public class MainActivity extends AppCompatActivity {

    private Socket mSocket;
    {
        try {
            mSocket = IO.socket("http://chatserver:3000");
            Log.e("CONNECTED", "SUCCESS");
        } catch (URISyntaxException e) {
            Log.e("ERROR", "ERROR CONNECTING");
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mSocket.connect();
        mSocket.emit("message", "message");
    }
}

When I compile this, no matter what I put as the URL for IO.socket(<url>), it always logs out that it connected. When I load the app, it's supposed to emit message and send "message", but I never get it on the server. I never even get a "user connected" log in my server log, which I have setup to log out anytime a something connects to it.

Just to test to make sure my server code isn't faulty, I created a client project in NodeJS:

client.js:

var io = require('socket.io-client'),
socket = io.connect('http://localhost:3000');

socket.on('connect', function(){
  console.log("connected")
});

And it and the server both log out 'connected' when I run it. So something is wrong with the java code.... I can't figure it out.

skparticles commented 8 years ago

Same like @joshterrill it doesn't work for me too. Never trigger connect but it triggers disconnect. Not sure how to fix it.

But iOS Socket.io is working on my server, only Android is not working. I suspect, it is the server side setting issue for android. Because, when I point this demo to my server. It didn't trigger connect.

nkzawa commented 8 years ago

I don't know if it's the cause but it looks the version of library is old.

 mSocket = IO.socket("http://chatserver:3000");
 Log.e("CONNECTED", "SUCCESS");

It doesn't mean the connection is successfully established even if a socket is created. You have to listen EVENT_CONNECT event.

Let me know if there are some errors(use EVENT_ERROR and EVENT_CONNECT_ERROR events) or logs.

afolabiolayinka commented 8 years ago

It may not work running from an emulator... make sure you can access the internet first.

lazarusvc commented 7 years ago

Are you running the server on a localhost?

afolabiolayinka commented 7 years ago
                                                                                  Oh... I have been able to resolve the issue. It was my firewalld (centos). All I did was enable the port I was using.                                                                                                                                                                                                                                                                                                                                        Sent from my BlackBerry 10 smartphone.                                                                                                                                                                                                                From: Austin LazarusSent: Tuesday, January 10, 2017 20:55To: nkzawa/socket.io-android-chatReply To: nkzawa/socket.io-android-chatCc: afolabiolayinka; CommentSubject: Re: [nkzawa/socket.io-android-chat] Problem with connecting to NodeJS server (#36)Are you running the server on a localhost?

—You are receiving this because you commented.Reply to this email directly, view it on GitHub, or mute the thread.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/nkzawa/socket.io-android-chat","title":"nkzawa/socket.io-android-chat","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/nkzawa/socket.io-android-chat"}},"updates":{"snippets":[{"icon":"PERSON","message":"@Lazarus118 in #36: Are you running the server on a localhost? "}],"action":{"name":"View Issue","url":"https://github.com/nkzawa/socket.io-android-chat/issues/36#issuecomment-271680285"}}}

mujtabazaidi commented 6 years ago

you need to give IP address of localhost in android "http://192.168.0.0:3000" instead of "http://chatserver:3000"

ezanalemma commented 4 years ago

from stackoverflow:

In the latest versions of Android, you need to add AndroidManifest.xml "android: usesCleartextTraffic = true" to allow connections with plain text.

Example:

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:usesCleartextTraffic="true">

https://stackoverflow.com/a/55916706

sanjeet007 commented 3 years ago

It is not working for android 10, 11 if i use normal socket.io that is working fine then what is problem in @nkzawa your api it is working in api 23