gpt4 book ai didi

javascript - Openlaszlo WebSocket 不工作

转载 作者:行者123 更新时间:2023-11-28 07:49:35 25 4
gpt4 key购买 nike

我尝试通过 WebSocket Api 连接我的 openlaszlo 应用程序。但它没有奏效。我在 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 吗?请建议我如何连接实时双向通信。提前致谢。

最佳答案

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>

引用文献

关于javascript - Openlaszlo WebSocket 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com