N
N
Nicholas Unknown2020-01-06 12:57:27
Java
Nicholas Unknown, 2020-01-06 12:57:27

How to properly terminate connection with java WebSocket?

In the presence of own-hand written server and client programs.

The server part is working with sockets.
The client part connects to this server.
In the client part, the code that is responsible for working with sockets has been moved to the service.
The service starts when the application starts.

Duplex communication works great. Everything connects and communicates perfectly.

BUT.
The client program has a "Close Connection" button
that calls the closeConnection() method;

And when I press the button on the server, I get such an EOFException. The reasons for its occurrence are not clear to me.

How to solve this problem and what is it?
All code is written for the purpose of the thesis.
I do not have good knowledge of programming.
I make a program and learn in the course of work.

So please don't throw stones :)

Server side code

@ServerEndpoint(value = "/server/{username}",
        encoders = MessageEncoder.class,
        decoders = MessageDecoder.class)
public class MessageServer {

    ChatRoom chatRoom = new ChatRoom();

    @OnOpen
    public void onOpen(Session session, @PathParam("username") String username) {
        chatRoom.getClients().put(username, session);
        chatRoom.sendBroadcast(new Message(username, "all", "Client" + username + " connected!"));

        System.out.println("OnOpen: Client: " + username + " connected! " + new Date());
    }

    @OnMessage
    public void onMessage(Session session, Message message) {
        chatRoom.sendBroadcast(message);
    }

    @OnClose
    public void onClose(Session session, @PathParam("username") String username){
        System.out.println("OnClose Client: " + username + " disconnected! " + new Date());
    }


    @OnError
    public void onError(Session session, Throwable throwable, @PathParam("username") String username) {
        System.out.println("OnError Client: " + username + " Exception: " + throwable + " " + new Date());
        throwable.printStackTrace();
    }

}


Part of the client-side service code (Connecting and working with sockets)
webSocketClient = new WebSocketClient(uri) {
            @Override
            public void onOpen() {
                Log.d(DebugUtils.TAG, "onOpen: OnOpen successfully!");
            }

            @Override
            public void onTextReceived(String json) {

                try {
                    Message message = gson.fromJson(json, Message.class);
                    Log.d(DebugUtils.TAG, "onTextReceived: " + message.getContent());


                    if (isBound) {
                        Log.d(DebugUtils.TAG, "onTextReceived: activity alive! sending broadcast");
                        Intent intent = new Intent(App.ACTION_MESSAGE_RECEIVED);
                        intent.putExtra(App.MESSAGE_EXTRA, message);
                        sendBroadcast(intent);

                    } else {
                        Log.d(DebugUtils.TAG, "onTextReceived: activity dead! making notification");

                        makeMessageNotification(message.getSender(), message.getContent());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d(DebugUtils.TAG, "onTextReceived: Exception!!!");
                }


            }

            @Override
            public void onBinaryReceived(byte[] data) {
                Log.d(DebugUtils.TAG, "onBinaryReceived: ");
            }

            @Override
            public void onPingReceived(byte[] data) {
                Log.d(DebugUtils.TAG, "onPingReceived: ");
            }

            @Override
            public void onPongReceived(byte[] data) {
                Log.d(DebugUtils.TAG, "onPongReceived: ");
            }

            @Override
            public void onException(Exception e) {

                Log.d(DebugUtils.TAG, "onException: " + e);
            }

            @Override
            public void onCloseReceived() {
                Log.d(DebugUtils.TAG, "onCloseReceived: ");
            }
        };

        webSocketClient.setConnectTimeout(10000);
        webSocketClient.setReadTimeout(300000);
        webSocketClient.enableAutomaticReconnection(5000);
        webSocketClient.connect();


Part of client side service code (Close connection)
public void closeConnection() {
        webSocketClient.close();
    }


Screenshot of server logs.

First I connect to the server
Then I press the close button on my phone

5e13031084630077587997.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-01-06
@OMGcoder

In niks everything is a file. When you close the connection, you close the file, and you are honestly told about it.
Wrap this exception in a catch block on the server.
Add a message to your log.
User such and such terminated the connection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question