I'm trying to push some monitoring message to my client jsf page using websocket. But from past 2days have come close to solve but still unable to push message to jsf page though I sucesseded in creating connection to web socket.
Here is my javaScript code.
websocket = new WebSocket("ws://localhost:9000/jdbc/monitor");
websocket.binaryType = 'arraybuffer';
websocket.onopen = function(evt) {
onOpen(evt);
};
websocket.onclose = function(evt) {
onClose(evt);
};
websocket.onmessage = function(evt) {
onMessage(evt);
};
websocket.onerror = function(evt) {
onError(evt);
};
function startSocket() {
websocket.send("Started");
}
function stopSocket() {
websocket.close();
}
function onOpen(evt) {
console.log("Connection opened" + evt);
}
function onClose(evt) {
console.log("Connection closed" + evt);
}
function onMessage(evt) {
console.log("Recieved message" + evt);
}
function onError(evt) {
console.log("error occured" + evt);
}
function doSend(message) {
console.log("sending message...");
websocket.send(reqmsg.value);
}
The CXF Rest Class
@Path("/jdbc/")
@Service
public class JdbcMonitor {
private final static Logger LOG = LoggerFactory.getLogger(JdbcMonitor.class);
private final Set<OutputStream> monitor = new HashSet<OutputStream>();
@GET
@Path("/monitor")
@Produces("text/*")
public StreamingOutput monitor() {
return new StreamingOutput() {
@Override
public void write(OutputStream wr) throws IOException,
WebApplicationException {
LOG.debug("Recieved a connection");
monitor.add(wr);
wr.write(("Regsitered for monitor at" + new java.util.Date()
.toString()).getBytes());
LOG.debug("Write completed");
}
};
}
public synchronized void sendData(final String message) {
LOG.debug("Triggered a websocket send");
int i=0;
for (final Iterator<OutputStream> it = monitor.iterator();it.hasNext();) {
LOG.debug("Sending number"+ i++);
final OutputStream out = it.next();
try {
out.write(message.getBytes());
} catch (IOException e) {
try {
out.close();
LOG.debug("Closing connection");
} catch (IOException e1) {
LOG.debug("Closing connection");
}
it.remove();
}
}
LOG.debug("Websocket send completed");
}
}
and the CXF bean class
<jaxrs:server id="kpWebSockServer" address="ws://localhost:9000/" >
<jaxrs:serviceBeans>
<ref bean="jdbcMonitor" />
</jaxrs:serviceBeans>
</jaxrs:server>
The code I'm using is similar to that of CXF sample provided. And I'm able to connect to the server. as FireFox shows me log Connection opened[object Event]
and after some time I recieve closed connection log as there is no communication between client-server. However log shows that jetty receives the message but fails to forward it to monitor method. Hence I believe I'm missing some jetty related configuration so that message can be delegated to CXF from jetty server(The entire set up is deployed in tomcat7 server, which does not support websocket). Could somebody help by pointing out the missing link. Here is jetty related log.
2014-09-24 21:06:23,997 [qtp10414855-16 Selector0] DEBUG org.eclipse.jetty.io.nio - created SCEP@c8ddda{l(/127.0.0.1:57569)<->r(/127.0.0.1:9000),s=0,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{AsyncHttpConnection@15d3a98,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}
2014-09-24 21:06:24,001 [qtp10414855-21] DEBUG org.eclipse.jetty.http.HttpParser - filled 468/468
2014-09-24 21:06:24,015 [qtp10414855-21 - /jdbc/monitor] DEBUG org.eclipse.jetty.server.Server - REQUEST /jdbc/monitor on AsyncHttpConnection@15d3a98,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=9,c=0},r=1
2014-09-24 21:06:24,016 [qtp10414855-21 - /jdbc/monitor] DEBUG o.e.j.server.handler.ContextHandler - scope null||/jdbc/monitor @ o.e.j.s.h.ContextHandler{,null}
2014-09-24 21:06:24,016 [qtp10414855-21 - /jdbc/monitor] DEBUG o.e.j.server.handler.ContextHandler - context=||/jdbc/monitor @ o.e.j.s.h.ContextHandler{,null}
2014-09-24 21:06:24,047 [qtp10414855-21 - /jdbc/monitor] DEBUG o.e.j.websocket.WebSocketFactory - extensions=[]
2014-09-24 21:06:24,094 [qtp10414855-21 - /jdbc/monitor] INFO o.a.c.t.w.jetty.JettyWebSocket - onOpen(WSFrameConnection@1977251 l(127.0.0.1:9000)<->r(127.0.0.1:57569)))
2014-09-24 21:06:24,095 [qtp10414855-21 - /jdbc/monitor] DEBUG o.e.j.websocket.WebSocketFactory - Websocket upgrade /jdbc/monitor 13 null WebSocketServletConnectionRFC6455 p=WebSocketParserRFC6455@1d2436e state=START buffer=null g=WebSocketGeneratorRFC6455@148fc1a closed=false buffer=-1
2014-09-24 21:06:24,095 [qtp10414855-21 - /jdbc/monitor] DEBUG org.eclipse.jetty.server.Server - RESPONSE /jdbc/monitor 101 handled=true
2014-09-24 21:06:24,096 [qtp10414855-21] DEBUG o.e.j.server.AsyncHttpConnection - Enabled read interest SCEP@c8ddda{l(/127.0.0.1:57569)<->r(/127.0.0.1:9000),s=1,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{AsyncHttpConnection@15d3a98,g=HttpGenerator{s=4,h=0,b=-1,c=-1},p=HttpParser{s=0,l=9,c=0},r=1}
2014-09-24 21:06:24,130 [qtp10414855-21] DEBUG org.eclipse.jetty.io.nio - WebSocketServletConnectionRFC6455 p=WebSocketParserRFC6455@1d2436e state=START buffer=null g=WebSocketGeneratorRFC6455@148fc1a closed=false buffer=-1 replaced AsyncHttpConnection@15d3a98,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=-3},r=1
Finally found my problem. Its was the problem with JavaScript code. In send method I should pass the URI with http method type the below code fixed this issue
function startSocket() {
websocket.send("Get /jdbc/monitor");
}