【问题标题】:Socket.io auto disconnect in AndroidAndroid中的Socket.io自动断开连接
【发布时间】:2017-08-13 04:03:19
【问题描述】:

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

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 连接权限。我不知道我的来源有什么问题。正如我所说的套接字连接在 Activity 中工作完美 我怎样才能解决我的问题? 谢谢

【问题讨论】:

    标签: android node.js sockets socket.io


    【解决方案1】:

    唯一对我有用的就是降级gradle中的socket版本

    使用这个并且有效

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

    【讨论】:

      【解决方案2】:
       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
      

      【讨论】:

      • 请添加此代码如何回答问题的说明。请在发布前阅读 SO 指南。
      【解决方案3】:

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

      只要这样做: socket = IO.socket("http://192.168.101.139:8080");

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

      同时将套接字设为静态。

      【讨论】:

      • 为什么?你能告诉我吗 ? @M.Saad Lakhan
      • 没有这些功能的文档 我遇到了问题,我这样做了,问题得到了解决。试试这个它会解决你的问题。希望如此:)
      • 使套接字也静态
      • 为给定的 URL 创建一个新的 Manager,并尝试为后续调用重用现有的 Manager,除非以 false 传递 multiplex 选项。传递此选项等效于传递 'force new connection': true 或 forceNew: true。为 URL 中路径名指定的命名空间返回一个新的 Socket 实例,默认为 /。例如,如果 url 是 localhost/users,则将建立到 localhost 的传输连接,并将建立到 /users 的 Socket.IO 连接。参考:socket.io/docs/client-api
      • 我更新了我的源代码,但仍然无法正常工作@M.Saad Lakhan
      猜你喜欢
      • 2013-04-06
      • 2014-01-02
      • 2012-05-28
      • 2012-11-09
      • 1970-01-01
      • 2017-07-17
      • 2016-07-08
      • 1970-01-01
      • 2021-11-18
      相关资源
      最近更新 更多