我试图使用websocket将一些监控消息推送到我的客户端jsf页面。但是从过去的2天开始已经接近解决但仍然无法将消息推送到jsf页面,尽管我已经开始创建与Web套接字的连接。
这是我的javaScript代码。
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);
}
CXF休息班
@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");
}
}
和CXF bean类
<jaxrs:server id="kpWebSockServer" address="ws://localhost:9000/" >
<jaxrs:serviceBeans>
<ref bean="jdbcMonitor" />
</jaxrs:serviceBeans>
</jaxrs:server>
我使用的代码与提供的CXF示例类似。我能够连接到服务器。因为FireFox向我显示了日志Connection opened[object Event]
,并且在一段时间后我收到了关闭的连接日志,因为客户端 - 服务器之间没有通信。但是日志显示jetty接收消息但未能将其转发到monitor方法。因此,我相信我错过了一些与码头相关的配置,因此可以从jetty服务器将消息委托给CXF(整个设置部署在tomcat7服务器中,不支持websocket)。有人可以通过指出缺失的链接来帮助。这是码头相关的日志。
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
答案 0 :(得分:2)
终于找到了我的问题。这是JavaScript代码的问题。在send方法中,我应该使用http方法类型传递URI,下面的代码修复了这个问题
function startSocket() {
websocket.send("Get /jdbc/monitor");
}