Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中的Socket.io自动断开连接_Android_Node.js_Sockets_Socket.io - Fatal编程技术网

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

Android中的Socket.io自动断开连接,android,node.js,sockets,socket.io,Android,Node.js,Sockets,Socket.io,我在Android中使用socket.io。我在node.js服务器中成功连接了我的Android应用程序。一切都很完美,但我尝试在Sturtup上创建连接,但socket自动断开连接 这是我的消息来源 public class AutoRunService extends BroadcastReceiver { private static Socket socket; @Override public void onReceive(Context context, Intent intent

我在Android中使用socket.io。我在node.js服务器中成功连接了我的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>

当然,我在清单中有internet连接权限。我不知道我的源代码中有什么错误。正如我所说的,套接字连接在活动中工作正常 我怎样才能解决我的问题? 谢谢

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

就这么做吧: 套接字=IO.socket(“”)

让我知道它是否有效。希望它能帮助您:)


也要使套接字保持静态。

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

 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
使用此功能并工作

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

为什么?你能告诉我吗@M.Saad Lakhanthere没有关于这些特性的文档,我有一个问题,我做了这个,问题已经解决了。尝试此操作将解决您的问题。希望如此:)Make socket static also为给定URL创建一个新的管理器,并尝试为后续调用重用现有管理器,除非使用false传递多路复用选项。传递此选项等同于传递“强制新连接”:true或forceNew:true。将为URL中路径名指定的命名空间返回一个新套接字实例,默认为/。例如,如果url为,则将建立到的传输连接和到/用户的Socket.IO连接。参考:我更新了我的源代码,但仍然不起作用@M.Saad lakhan请添加此代码如何回答问题的解释。请在发布前阅读SO指南。