java – Establishing a continuous connection to the server

Question:

Hello, there is a code, it sends hello using web sockets to the server and immediately ends the connection. I'm trying to make sure that the connection is not interrupted but continues to listen and wait for the next message. Here is the code:

public class MainActivity extends AppCompatActivity {
private Button start;
    private TextView out;
    private OkHttpClient client;
    private  final class Echo extends WebSocketListener{
        private static final int NORMAL_CLOSURE_STATUS = 1000;
        @Override
        public void onOpen(WebSocket webSocket, Response response) {
           webSocket.send("hello");

        webSocket.close(NORMAL_CLOSURE_STATUS,"bye");
        }

        @Override
        public void onMessage(WebSocket webSocket, String text) {
            out("R"+text);
        }



        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            out("error"+t.getMessage());
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        start =(Button)findViewById(R.id.button);
        out=(TextView)findViewById(R.id.textView);
        client= new OkHttpClient();



        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                start();
            }
        });

    }
    private void start(){

       Request request = new Request.Builder().url("ws://адрес:8081").build();
        Echo listener = new Echo();
       WebSocket ws = client.newWebSocket(request, listener);

       //client.dispatcher().executorService().shutdown();
    }

private void out(final String txt){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            out.setText(out.getText().toString()+"\n\n"+ txt);
        }
    });

}

}

I tried to remove the line webSocket.close(NORMAL_CLOSURE_STATUS,"bye"); it worked, but with each next click on the "send" button, the number of "hello" messages increased by one, that is, I click once, 2 messages are sent, I click again – 3 are sent. Tell me what is the problem?

Answer:

Until someone smarter came, I will offer you an experiment:

    @Override
    public void onOpen(WebSocket webSocket, Response response) {
       int i = 0;
       webSocket.send("hello" + i++);
       webSocket.send("hello" + i);

    webSocket.close(NORMAL_CLOSURE_STATUS,"bye");
    }

Do this and press 2-3 times. I'm wondering if the socket stores messages in a buffer and duplicates them, or if some part of the code calls the method 2 times.


Try removing webSocket.close (NORMAL_CLOSURE_STATUS, "bye"); can be onClosed to onClosed or onClosing .

Later

   Request request = new Request.Builder().url("ws://адрес:8081").build();
    Echo listener = new Echo();
   WebSocket ws = client.newWebSocket(request, listener);

put it into class variables, because with each click, new objects are created for you and apparently the WebSocketListener processes all available WebSocket.

Scroll to Top