【问题标题】:Android Socket.io emits twice instead of once?Android Socket.io 发出两次而不是一次?
【发布时间】:2019-03-11 11:06:24
【问题描述】:

我正在使用 android socket.io 但问题是发出事件运行两次而不是一次,我的意思是我在 onCreate 方法中只编写了一个发出代码,但它向服务器发送了两个请求? 我搜索了很多,但没有找到任何东西。

我在后端使用node js,我的代码没有任何问题。

socket.io for android 中是否存在错误?

这是我的代码:

    SocketManager.getInstance().connect();
    // Creating Bids
    final Handler mHandler04 = new Handler(Looper.getMainLooper());
    mHandler04.post(new Runnable() {
        @Override
        public void run() {
            SocketManager.getInstance().getSocket().emit("allc", "some");
            SocketManager.getInstance().getSocket().on("allcRes", new Emitter.Listener() {
                @Override
                public void call(final Object... args) {
                    g.context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            JSONArray jsonArray = (JSONArray) args[0];
                            arrayComps.clear();
                            Log.d(TAG, "run: " + jsonArray);
                            try {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    createView(jsonArray.getJSONObject(i).getString("title"), jsonArray.getJSONObject(i).getString("realprice"), jsonArray.getJSONObject(i).getString("id"), jsonArray.getJSONObject(i).getString("starttime"), jsonArray.getJSONObject(i).getString("img"));
                                    CustomViewCompetition css  = (CustomViewCompetition) LinearLayoutItemHolder.getChildAt(i);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            });
        }
    });
    final Handler bidsupdateHandler = new Handler(Looper.getMainLooper());
    bidsupdateHandler.post(new Runnable() {
        @Override
        public void run() {
            SocketManager.getInstance().getSocket().on("bidsupdate", new Emitter.Listener() {
                @Override
                public void call(final Object... args) {
                    final JSONArray jsonArray = (JSONArray) args[0];
                    g.context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Log.d(TAG, "run in bidsupdate");
                            try {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    bidsMap.put(jsonArray.getJSONObject(i).getInt("key"), jsonArray.getJSONObject(i).getInt("value"));
                                }

                                for (int i = 0; i < LinearLayoutItemHolder.getChildCount(); i++) {
                                    CustomViewCompetition cs = (CustomViewCompetition) LinearLayoutItemHolder.getChildAt(i);
                                    Log.d(TAG, "run in cs " + cs.txtCsRealPrice.getText());

                                }


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

                }
            });
        }
    });

问题是当我得到 cs.txtCsTitle.getText() 时,它只是显示其中之一

【问题讨论】:

  • 你在哪里写这段代码?我的意思是 onCreate 方法或 onResume 等?
  • @M.SaadLakhan 我在 onCreate 方法中编写这些代码

标签: android node.js socket.io


【解决方案1】:

添加这一行:

SocketManager.getInstance().getSocket().emit("allc", "some");

紧接着:

SocketManager.getInstance().connect();

所以你的代码会是这样的

SocketManager.getInstance().connect();
SocketManager.getInstance().getSocket().emit("allc", "some");

并从您已经在处理程序中使用的位置删除。你也不需要处理程序为什么你使用处理程序?创建您的侦听器并从需要创建或订阅一次的处理程序中发出您的订阅。如果您需要更新任何视图,则在创建发射器侦听器传递视图时。然后在此处更新视图,您可能需要 runnable 来更新 uiThread 上的 UI。

创建类如下:

public class YourListener implements Emitter.Listener
{
private yourView view;

public YourListener (View view)
{
    this.view = view;
}

@Override
public void call(Object... args)
{
    // do your work here
}
}

并替换以下代码:

SocketManager.getInstance().getSocket().on("allcRes", new Emitter.Listener() {
            @Override
            public void call(final Object... args) {
                g.context.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        JSONArray jsonArray = (JSONArray) args[0];
                        arrayComps.clear();
                        Log.d(TAG, "run: " + jsonArray);
                        try {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                createView(jsonArray.getJSONObject(i).getString("title"), jsonArray.getJSONObject(i).getString("realprice"), jsonArray.getJSONObject(i).getString("id"), jsonArray.getJSONObject(i).getString("starttime"), jsonArray.getJSONObject(i).getString("img"));
                                CustomViewCompetition css  = (CustomViewCompetition) LinearLayoutItemHolder.getChildAt(i);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

与:

SocketManager.getInstance().getSocket().on("your_subscription", new YourListener (yourView));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多