Ok. I made a minimal example for client and and server. I attachted it in
this post. (
http://www.java.net/sites/default/files/example.zip)
Are there extra libraries for Java websocket-clients? Are
weberknecht-websocket client libraries are not compatible in glassfish?
Here are my minimal examples:
*Client* (work with jetty, but not with glassfish):
package de.wsclient;
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.110:8080/WSTest/WebServletTest");
// glassfish url
URI url = new
URI("ws://172.30.17.110: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(arg0.getText());
open = false;
}
@Override
public void onClose() {
System.out.println("Websockt closed");
}
});
socket.connect();
socket.send("test");
// wait for incoming messages, then close connection
while(open){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch
block./as
e.printStackTrace();
}
}
socket.close();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (WebSocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
GlassFish *WebSocket Servlet:*
/Servlet:/
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;
/**
* Servlet implementation class WebSocketServlet
*/
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 der " + 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
}
}
/WebSocketApplication-Class:/
package de.gfwebsocket;
import com.sun.grizzly.tcp.Request;
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 createSocket(WebSocketListener... listeners) {
// TODO Auto-generated method stub
//return super.createSocket(listeners);
return new WebSocketDataProcess(listeners);
}
// @Override
// public void onClose(WebSocket socket) {
// // TODO Auto-generated method stub
// super.onClose(socket);
// }
//
// @Override
// public void onConnect(WebSocket socket) {
// // TODO Auto-generated method stub
// super.onConnect(socket);
// }
@Override
public void onMessage(WebSocket socket, String text) {
socket.send(text);
//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;
}
}
/BaseServerWebSocket:/
package de.gfwebsocket;
import com.sun.grizzly.websockets.BaseServerWebSocket;
import com.sun.grizzly.websockets.WebSocketListener;
public class WebSocketDataProcess extends BaseServerWebSocket{
public WebSocketDataProcess(WebSocketListener... listeners) {
// TODO Auto-generated constructor stub
super(listeners);
}
}
My approach for this examples:
1. ./asadmin start-domain
2. ./asadmin set
configs.config.server-config.network-config.protocols.protocol.http-listener-1.http.websockets-support-enabled=true
3. ./asadmin stop-domain
4. ./asadmin start-domain
5. run the client
thank you for your help in advance
best regards
hsch
--
[Message sent by forum member 'hsch']
View Post: http://forums.java.net/node/882757