Android中的Socket.io自动断开连接

时间:2017-03-21 13:02:28

标签: android node.js sockets socket.io

我在Android中使用socket.io.I在node.js server中成功连接我的android应用程序。一切都很完美,但我尝试在Sturtup上创建连接,但socket自动断开连接 这是我的来源

public class AutoRunService extends BroadcastReceiver {
private static Socket socket;
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Toast.makeText(UApplication.getInstance(), "Application is ready to  open ", Toast.LENGTH_SHORT).show();
        seSocketConnection(context);

    }
}


public void seSocketConnection(final Context context) {
    try {

        socket = IO.socket("http://192.168.101.139:8080");
        socket.on("onconnect", new Emitter.Listener() {

                    @Override
                    public void call(Object... args) {
                        JSONObject obj = new JSONObject();
                        Log.e("AutoRunService", "onconnect");

                        try {
                            obj.put("host", "************");
                            obj.put("entity", new DeviceManager(UApplication.getInstance()).getDeviceId());
                            socket.emit("initialize", obj);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }
        ).on("onerror", new Emitter.Listener() {

                    @Override
                    public void call(Object... args) {
                        Log.e("AutoRunService", "onerror");


                    }
                }
        ).on("device", new Emitter.Listener() {

                    @Override
                    public void call(Object... args) {
                        Log.e("AutoRunService", "device");

                        Intent i = new Intent(context, WelcomeImageActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(i);


                    }
                }
        ).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                Log.e("AutoRunService", "EVENT_CONNECT_ERROR");
            }


        }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
              //  socket.disconnect();
                Log.e("AutoRunService", "EVENT_DISCONNECT");

            }

        });
        socket.connect();

    } catch (URISyntaxException e) {
        e.printStackTrace();
        Log.e("AutoRunService", e.toString());

    }
}

}

清单源代码

 <receiver
        android:name=".AutoRunService"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

当然,我在清单中有互联网连接权限。我不知道我的源代码有什么问题。正如我所说套接字连接在Activity中工作完美 我怎么能解决我的问题? 感谢

3 个答案:

答案 0 :(得分:0)

你不需要这样做 opts.forceNew = true; opts.reconnection = true;

就这样做: socket = IO.socket(&#34; http://192.168.101.139:8080&#34;);

让我知道它是否有效。希望它可以帮助你:)

也使插座静止。

答案 1 :(得分:0)

 socket.on("onconnect", new Emitter.Listener() {

                @Override
                public void call(Object... args) {
                    JSONObject obj = new JSONObject();
                    Log.e("AutoRunService", "onconnect");

                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {}

                    try {
                        obj.put("host", "************");
                        obj.put("entity", new DeviceManager(UApplication.getInstance()).getDeviceId());
                        socket.emit("initialize", obj);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }
    )enter code here

答案 2 :(得分:0)

唯一有效的方法是降级socket中的gradle版本

使用且有效

 implementation('io.socket:socket.io-client:0.8.3') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }