Openlaszlo WebSocket not working



我试图将我的openlaszlo应用程序连接到WebSocket API。但它没有用。我在 lzx 文件中使用了 javascript。同样的JavaScript作为纯HTML文件工作。但它在 lzx 文件中不起作用。

<class name="SpecialButton" extends="button" onclick="changeLabel()">
    <attribute name="changeToLabel" value="Clicked!" type="string"/>
    <method name="changeLabel">
        var newLabel = openFunction(this.changeToLabel);
        //this.setAttribute('text', newLabel);
        openSocket();
    </method>

</class>
<script> <![CDATA[
        var webSocket;
        var messages = "Hello Web Socket";
        function openFunction(arg){
            return arg+" done";
        }
        function openSocket() {
            // Ensures only one connection is open at a time
            if (webSocket !== undefined
                    && webSocket.readyState !== WebSocket.CLOSED) {
                writeResponse("WebSocket is already opened.");
                return;
            }
            // Create a new instance of the websocket
            webSocket = new WebSocket("ws://localhost:8888/HelloSocket/echo");
            //Debug.debug(WebSocket.CLOSED);
            /**
             * Binds functions to the listeners for the websocket.
             */
            webSocket.onopen = function(event) {
                // For reasons I can't determine, onopen gets called twice
                // and the first time event.data is undefined.
                // Leave a comment if you know the answer.
                if (event.data === undefined)
                    return;
                writeResponse(event.data);
            };
            webSocket.onmessage = function(event) {
                writeResponse(event.data);
            };
            webSocket.onclose = function(event) {
                writeResponse("Connection closed");
            };
        }
        /**
         * Sends the value of the text input to the server
         */
        function send() {
            var text = "Hello World!!";
            webSocket.send(text);
        }
        function closeSocket() {
            webSocket.close();
        }
        function writeResponse(textmessage) {
            //result.setAttribute("text", textmessage);
            this.setAttribute('text', textmessage);
        }
        ]]>
    </script>
<simplelayout axis="y" spacing="10"/>
<SpecialButton>Not clicked</SpecialButton>
<SpecialButton changeToLabel="Thank You!">Please click me!</SpecialButton>

调试输出如下所示:

ERROR @helloClass.lzx#72: reference to undefined variable 'webSocket'
ERROR @helloClass.lzx#78: call to undefined function 'WebSocket' 
ERROR @helloClass.lzx#83: reference to undefined variable 'webSocket' 
ERROR @helloClass.lzx#83: undefined object does not have a property 'onopen' 
ERROR @helloClass.lzx#93: reference to undefined variable 'webSocket' 
ERROR @helloClass.lzx#93: undefined object does not have a property 'onmessage' 
ERROR @helloClass.lzx#97: reference to undefined variable 'webSocket' 
ERROR @helloClass.lzx#97: undefined object does not have a property 'onclose' 

在 lzx 或 openlaszlo 中不支持 WebSocket API 吗?请建议我如何连接实时 2 路通信。提前谢谢。

Openlaszlo支持两种形式的实时通信:

  • RTMP 通过 red5
  • XML Sockets via flash.net.XMLSocket

例如,下面是一个使用 XMLSocket 的类:

 <class name="ClientSocket" extends="node">
  <attribute name="host" />
  <attribute name="port" />
  <attribute name='xml_socket'/>
  <handler name="oninit">
    xml_socket = new XMLSocket();
    // connect the socket here:
    xml_socket.connect(host,port);
  </handler>
  <handler name='onData' reference='xml_socket' args='messageXML'>
   <![CDATA[
    ExternalInterface.call(‘handleServerMessageReceived',messageXML);
   ]]>
  </handler>    
 </class>

以及它的一个实例:

<canvas>
 <ClientSocket id='serverPushSocket' host='localhost' port='20340'/>
</canvas>

引用

  • 使用 OpenLaszlo 打开客户端套接字
  • OpenLaszlo -- 快速构建和部署富互联网应用程序的平台
  • OpenLaszlo Source

相关内容

  • 没有找到相关文章

最新更新