如何从websocket发送收到的消息到客户端的支持bean

时间:2014-08-19 12:36:09

标签: javascript primefaces websocket jsf-2.2

如何使用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>

0 个答案:

没有答案