Websocket client connection taking some 5 seconds

259 views Asked by At

Some developer has developed Mobile App which sends specific SMS to a remote Websocket server.

For many mobiles it's quick enough to connect and send the SMS. However there are a few mobiles where it's taking 5-10 seconds extra to deliver the SMS.

It is the time when it is received in mobile(i hear the sound) and the time the websocket server actually receives it.

What could be slowing down in this code at least in Oneplus 6 I've :

WebSocketClient mWebSocketClient = new WebSocketClient(uri) {
                    @Override
                    public void onOpen(ServerHandshake serverHandshake) {
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("op", "saveotp");
                            jsonObject.put("mobile", CachedData.getString(CachedData.login, ""));
                            jsonObject.put("email", CachedData.getString(CachedData.email, ""));
                            jsonObject.put("vehicle", CachedData.getString(CachedData.vehicle, ""));
                            jsonObject.put("custom1", CachedData.getString(CachedData.custom, ""));
                            jsonObject.put("l", "aabffffdee");
                            jsonObject.put("source", 999999);
                            String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
                            jsonObject.put("version", "native" + version);
                            jsonObject.put("count", count2);
                            jsonObject.put("sms", smsData.message);
                            jsonObject.put("received", smsData.received_time);
                            jsonObject.put("sent", StringHelper.getCurrentStringTime());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        Log.e(TAG, jsonObject.toString());
                        this.send(jsonObject.toString());
                    }

                    @Override
                    public void onMessage(String s) {
                        Log.e("onMessage", s);
                        Log.e(TAG, "Sent to WSS2");
                        if (s.equals("otpreceived")) {
                            count2 = 0;
                            smsData.isSentWS2 = true;
                            DBManager.getInstance().updateSmsData(smsData);
                            DBManager.getInstance().checkSmsData(smsData.id);
                        }
                        isSyncWS2 = false;
                    }

                    @Override
                    public void onClose(int i, String s, boolean b) {
                        count2++;
                        Log.e(TAG, "Count 2: " + count2);
                        isSyncWS2 = false;
                    }

                    @Override
                    public void onError(Exception e) {
                        isSyncWS2 = false;
                        Log.e(TAG, "Error 2: " + e.getLocalizedMessage());
                    }
                };
                try {
                    SSLContext sslContext = SSLContext.getInstance("TLS");
                    sslContext.init(null, trustAllCerts, new SecureRandom());
                    SSLSocketFactory factory = sslContext.getSocketFactory();
                    mWebSocketClient.setSocket(factory.createSocket());
                    mWebSocketClient.connect();
                    mWebSocketClient.setConnectionLostTimeout(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

I searched but I see nothing which could be slowing down in this code. I assume the remote Websocket server is quick enough and delay is not due to it.

0

There are 0 answers