Openlaszlo WebSocket无法正常工作

时间:2014-11-18 23:39:12

标签: javascript websocket openlaszlo

我尝试将我的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吗?请建议我如何连接实时双向通信。提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

例如,这是一个使用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>

<强>参考