如何使用websocket,javascript客户端和ServerEndpoint
发送和接收对象。
在客户端,我想发送一个param到backing bean方法。
客户方:
<script type="text/javascript">
var host = "ws://localhost:8030/nytp/menu_notification";
var wSocket = new WebSocket(host);
var browserSupport = ("WebSocket" in window) ? true : false;
// called body onLoad()
function initializeReception()
{
if (browserSupport)
{
wSocket.onopen = function()
{
//alert(" Web Socket is connected, sending data");
wSocket.send("ping");
};
}
else
{
// The browser doesn't support WebSocket
alert("WebSocket is NOT supported by your Browser!");
}
}
// called when a message is received
wSocket.onmessage = function(event)
{
var received_msg = event.data;
//here I want to send 'received_msg' to backingbean method
};
// called when socket closes
wSocket.onclose = function()
{
// websocket is closed.
//alert("Connection is closed...");
};
</script>