java websocket开发的客户端程序
最近用java websocket开发的客户端程序,在和服务端链接通后,在数据传输完毕后,客户端自动关闭了链接,如何能保持链接不断开
这个是客户端的启动类,在循环完毕后,会自动断开和服务器的链接,开始怀疑是session超时问题,然后设置了下maxsession,却依然在没有数据传输后立刻断开了链接
public class ClientStart {
public static void main(String[] args){
try {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
MyClient client = new MyClient();
container.connectToServer(client, new URI("ws://localhost:8088/websocket"));
// container.setDefaultMaxSessionIdleTimeout(5000L);
int turn = 0;
while(turn++ < 10){
client.send("client send: " + turn);
Thread.sleep(1000);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
MyClient也放上去吧:
@ClientEndpoint
public class MyClient {
private static Logger logger = LoggerFactory.getLogger(MyClient.class);
private Session session;
@OnOpen
public void open(Session session){
logger.info("Client WebSocket is opening...");
this.session = session;
}
@OnMessage
public void onMessage(String message){
logger.info("Server send message: " + message);
}
@OnClose
public void onClose(){
logger.info("Websocket closed");
}
/**
* 发送客户端消息到服务端
* @param message 消息内容
*/
public void send(String message){
this.session.getAsyncRemote().sendText(message);
}
}