未能分析从服务器收到的消息,WebSocket Javascript客户端未收到来自服务器的消息...
我已經(jīng)在本地GlassFish 4.1服務(wù)器上部署了Java Web應(yīng)用程序,該服務(wù)器實(shí)現(xiàn)了與Web客戶端互操作的WebSockets。我能夠通過(guò)套接字成功執(zhí)行客戶端到服務(wù)器的通信,但由于某種原因,服務(wù)器到客戶端的通信不起作用。WebSocket Javascript客戶端未收到來(lái)自服務(wù)器的消息
將消息發(fā)送到客戶端的Java代碼:
try
{
String msg = ServerClientInteropManager.toResponseJSON(response);
parentSession.getBasicRemote().sendText(msg);
FLAIRLogger.get().info("Sent response to client. Message: " + msg);
}
catch (IOException ex) {
FLAIRLogger.get().error("Couldn't send message to session " + parentSession.getid() + ". Exception - " + ex.getMessage());
}
的JavaScript代碼:
pipeline_internal_onMessage = function(event)
{
var msg = JSON.parse(event.data);
console.log("Received message from server. Data: " + event.data);
};
function pipeline_init()
{
if (PIPELINE !== null || PIPELINE_CONNECTED === true)
{
console.log("Pipline already initialized");
return false;
}
else
{
var pipelineURI = "ws://" + document.location.host + document.location.pathname + "webranker";
console.log("Attempting to establish connection with WebSocket @ " + pipelineURI);
if ('WebSocket' in window)
PIPELINE = new WebSocket(pipelineURI);
else if ('MozWebSocket' in window)
PIPELINE = new MozWebSocket(pipelineURI);
else
{
console.log("FATAL: No WebSockets support");
alert("This browser does not support WebSockets. Please upgrade to a newer version or switch to a browser that supports WebSockets.");
return false;
}
// the other event listeners get added here
PIPELINE.onMessage = pipeline_internal_onMessage;
PIPELINE_CONNECTED = true;
window.onbeforeunload = function() {
pipeline_deinit();
};
console.log("Pipeline initialized");
return true;
}
}
在onMessage功能從來(lái)沒(méi)有發(fā)射,即使在服務(wù)器成功調(diào)用sendText()方法。使用AsyncRemote產(chǎn)生相同的結(jié)果。兩端的onError偵聽器也不會(huì)報(bào)告任何內(nèi)容。這是我第一次使用套接字,所以我可能會(huì)錯(cuò)過(guò)一些基本的東西。
2016-03-05
shadeMe
總結(jié)
以上是生活随笔為你收集整理的未能分析从服务器收到的消息,WebSocket Javascript客户端未收到来自服务器的消息...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: pi数据库系统中国服务器,基于PI数据库
- 下一篇: 服务器2012r2系统远程登陆,服务器2