简体   繁体   中英

Openlaszlo WebSocket not working

I tried to connect my openlaszlo application be WebSocket Api. But It did not work. I used javascript in lzx file. the same javascript is working as a plain html file. But it is not working in lzx file.

<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>

The Debug Output is given bellow:

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' 

Is WebSocket Api not supported in lzx or openlaszlo? Please suggest me how I will connect real time 2 way communication. Thanks in advance.

Openlaszlo supports two forms of real-time communication:

For example, here is a class which uses 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>

and an instance of it:

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

References

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM