[英]WebSocket Javascript client not receiving messages from server
我已經在本地GlassFish 4.1服務器上部署了Java Web應用程序,該服務器實現了WebSockets與Web客戶端進行互操作。 我能夠通過套接字成功執行客戶端到服務器的通信,但是由於某些原因服務器到客戶端的通信不起作用。
將消息發送到客戶端的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;
}
}
即使服務器成功調用sendText()方法,也不會觸發onMessage函數。 使用AsyncRemote會產生相同的結果。 兩端的onError偵聽器也不報告任何內容。 這是我第一次使用套接字,因此我可能缺少一些基本知識。
更換
PIPELINE.onMessage = pipeline_internal_onMessage
同
PIPELINE.onmessage = pipeline_internal_onMessage
請參考這里更多。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.