Answer the question
In order to leave comments, you need to log in
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();
}
}
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();
public void closeConnection() {
webSocketClient.close();
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question