首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Openlaszlo WebSocket不工作

Openlaszlo WebSocket不工作
EN

Stack Overflow用户
提问于 2014-11-19 07:39:12
回答 1查看 126关注 0票数 1

我尝试将我的openlaszlo应用程序连接到WebSocket Api。但它并没有起作用。我在lzx文件中使用了javascript。相同的javascript与普通的html文件一样工作。但它在lzx文件中不起作用。

代码语言:javascript
复制
<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>

Debug输出如下:

代码语言:javascript
复制
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?请建议我如何连接实时双向通信。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-12-19 00:21:39

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

通过flash.net.XMLSocketred5

  • XML套接字的
  • RTMP

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

代码语言:javascript
复制
 <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>

以及它的一个实例:

代码语言:javascript
复制
<canvas>
 <ClientSocket id='serverPushSocket' host='localhost' port='20340'/>
</canvas>

参考

  • Using OpenLaszlo to open a client socket
  • OpenLaszlo -- A platform to rapidly build and deploy rich Internet applications
  • OpenLaszlo Source
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27006294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档