Hi,
i try so timplement a simple chat application using glassfish version
3.1.2-b18 (build 18).
If two clients connect, they can write a message and the other client will
get the message. The getWebSockets() -Method returns 2. So far, so good.
But if one client disconnect, the getWebSockets()-method still returns 2. If
the disconnected client connect again, the getWebSockets()-method will return
3 and the clients can not send any messages. I do not know how to handle this
behavior. Only a restart of the server solve this problem and everything
works fine until the a client disconnect.
The following approach produces the behaviour:
1. C1 connect
2. C1 send text --> retrieve 1 (from getWebSockets()) (Client can send
message as he like to)
3. C1 disconnect
4. C1 connect
5. C1 send text --> retrieve 2 (from getWebSockets())
6. C1 cannot send messages after that
This is my implementation:
*Client *(with weberknecht-websockets)
package de.wsclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import de.roderick.weberknecht.WebSocket;
import de.roderick.weberknecht.WebSocketConnection;
import de.roderick.weberknecht.WebSocketEventHandler;
import de.roderick.weberknecht.WebSocketException;
import de.roderick.weberknecht.WebSocketMessage;
public class Client {
static boolean open = true;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// jetty url
//URI url = new
URI("ws://172.30.17.100:8080/WSTest/WebServletTest");
// glassfish url
URI url = new
URI("ws://172.30.17.100:8080/GlassfishWebSocket/WebSocketServlet");
WebSocket socket = new WebSocketConnection(url);
socket.setEventHandler(new WebSocketEventHandler() {
@Override
public void onOpen() {
System.out.println("Websocket opened");
}
@Override
public void onMessage(WebSocketMessage arg0) {
System.out.println("\tServer: " +
arg0.getText());
open = false;
}
@Override
public void onClose() {
System.out.println("Websocket closed!");
}
});
socket.connect();
BufferedReader buf = new BufferedReader(new
InputStreamReader(System.in));
String input = "";
while(!input.equals("stop")){
try {
input = buf.readLine();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
socket.send(input);
}
socket.close();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (WebSocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*Servlet* (set loadOnStartup=1 or <load-on-startup>1</load-on-startup> in
web.xml)
package de.gfwebsocket;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.grizzly.websockets.WebSocketEngine;
public class WebSocketServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private final WebSocketConnection conn = new WebSocketConnection();
public WebSocketServlet() {
super();
// TODO Auto-generated constructor stub
}
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
WebSocketEngine.getEngine().register(conn);
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy
HH:mm:ss");
PrintWriter writer = response.getWriter();
writer.println("<html>");
writer.println("<head><title>Hello World
Servlet</title></head>");
writer.println("<body>");
writer.println("<h1>Es ist " + format.format(new Date()) + "
Uhr</h1>");
writer.println("<body>");
writer.println("</html>");
writer.flush();
writer.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
*WebSocketConnection*
package de.gfwebsocket;
import com.sun.grizzly.tcp.Request;
import com.sun.grizzly.websockets.DataFrame;
import com.sun.grizzly.websockets.ProtocolHandler;
import com.sun.grizzly.websockets.WebSocket;
import com.sun.grizzly.websockets.WebSocketApplication;
import com.sun.grizzly.websockets.WebSocketListener;
public class WebSocketConnection extends WebSocketApplication{
@Override
public WebSocket createWebSocket(ProtocolHandler protocolHandler,
WebSocketListener... listeners) {
// TODO Auto-generated method stub
//return super.createWebSocket(protocolHandler, listeners);
return new WebSocketDataProcess(protocolHandler, listeners);
}
@Override
public void onClose(WebSocket socket, DataFrame frame) {
// TODO Auto-generated method stub
socket.close();
super.onClose(socket, frame);
}
@Override
public void onConnect(WebSocket socket) {
// TODO Auto-generated method stub
super.onConnect(socket);
}
@Override
public void onMessage(WebSocket socket, String text) {
for(final WebSocket sock : getWebSockets()){
//if(!(sock.equals(socket))){
//sock.send(text);
sock.send(String.valueOf(getWebSockets().size()));
//}
}
//super.onMessage(socket, text);
}
// return true if you want to communicate over websocket otherwise
return false
@Override
public boolean isApplicationRequest(Request arg0) {
// TODO Auto-generated method stub
return true;
}
}
*WebSocketProcess*
package de.gfwebsocket;
import com.sun.grizzly.websockets.ProtocolHandler;
import com.sun.grizzly.websockets.DefaultWebSocket;
import com.sun.grizzly.websockets.WebSocketListener;
//public class WebSocketDataProcess extends BaseServerWebSocket{
public class WebSocketDataProcess extends DefaultWebSocket{
public WebSocketDataProcess(ProtocolHandler handler,
WebSocketListener... listeners) {
// TODO Auto-generated constructor stub
super(handler, listeners);
}
}
How can i fix this problem? The servers seems not to delete or close the
server-side websocket or close the connection.
Thank your for your help in advance!
best regards
hsch
--
[Message sent by forum member 'hsch']
View Post: http://forums.java.net/node/882964