我的桌面应用客户端通过webSockets
连接到网络服务器。我正在使用javaWebSocket
api作为客户端,我正在使用Apache tomcat 7的webSocketServlet
。我不知道我做错了什么,但客户端与网络服务器完美连接并发送聊天消息但是在闲置一段时间后,服务器会自动断开客户端的连接。我希望客户端连接,直到客户端故意不想断开连接。以下是我的web Socket
servlet和客户端的示例代码。首先是客户端代码
Draft[] drafts = { new Draft_10(), new Draft_17(), new Draft_76(), new Draft_75() };
cc = new WebSocketClient( new URI( uriField.getText() ), (Draft) draft.getSelectedItem() ) {
@Override
public void onMessage( String message ) {
ta.append( "got: " + message + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
}
@Override
public void onOpen( ServerHandshake handshake ) {
ta.append( "You are connected to ChatServer: " + getURI() + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
}
@Override
public void onClose( int code, String reason, boolean wasClean ) {
ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" );
System.out.println("the reason for disconnection is ........ "+wasClean);
ta.setCaretPosition( ta.getDocument().getLength() );
connect.setEnabled( true );
uriField.setEditable( true );
draft.setEditable( true );
close.setEnabled( false );
}
@Override
public void onError( Exception ex ) {
ta.append( "Exception occured ...\n" + ex + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
ex.printStackTrace();
connect.setEnabled( true );
uriField.setEditable( true );
draft.setEditable( true );
close.setEnabled( false );
}
};
close.setEnabled( true );
connect.setEnabled( false );
uriField.setEditable( false );
draft.setEditable( false );
cc.connect();
} catch ( URISyntaxException ex ) {
ta.append( uriField.getText() + " is not a valid WebSocket URI\n" );
}
} else if( e.getSource() == close ) {
cc.close();
}
现在这是servlet代码。
private static ArrayList<MyStreamBound> mmiList = new ArrayList<MyStreamBound>();
@Override
protected StreamInbound createWebSocketInbound(String protocol) {
return new MyStreamBound();
}
private class MyStreamBound extends StreamInbound{
WsOutbound myoutbound;
@Override
public void onOpen(WsOutbound outbound){
try {
System.out.println("Open Client.");
this.myoutbound = outbound;
mmiList.add(this);
outbound.writeTextMessage(CharBuffer.wrap("Hello!"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void onBinaryData(InputStream arg0) throws IOException {
// TODO Auto-generated method stub
}
@Override
protected void onTextData(Reader recievedData) throws IOException {
BufferedReader in = new BufferedReader(recievedData);
String line = null;
StringBuilder rslt = new StringBuilder();
while ((line = in.readLine()) != null) {
rslt.append(line);
}
System.out.println(rslt.toString());
for(MyStreamBound mmib: mmiList){
CharBuffer buffer = CharBuffer.wrap(rslt.toString());
mmib.myoutbound.writeTextMessage(buffer);
mmib.myoutbound.flush();
}
}
@Override
protected void onClose(int status){
System.out.println("Close Client."+status);
mmiList.remove(this);
}
答案 0 :(得分:1)
答案 1 :(得分:1)
最后我设法解决了这个问题。问题出在tomcat的配置文件中。您必须转到以下目录ApacheTomcat-&gt; Config-&gt; server.xml,有连接超时名称的属性默认值为20000Ms(20秒),将其更改为-1表示无限连接时间。