我试图在Node.js服务器和Flash(Flex)应用程序之间建立套接字连接。我有一些意想不到的结果,我无法自己解释。
这是我的代码。
Flex:
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="windowedapplication1_creationCompleteHandler(event)">
<fx:Script>
<![CDATA[
import com.pnwrain.flashsocket.FlashSocket;
import com.worlize.websocket.WebSocket;
import mx.events.FlexEvent;
private var _flashSocket:FlashSocket;
private var _webSocket:WebSocket;
private var _xmlSocket:XMLSocket;
protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
{
_xmlSocket = new XMLSocket();
_xmlSocket.addEventListener(Event.CLOSE, function():void{trace('close')});
_xmlSocket.addEventListener(DataEvent.DATA, onData);
_xmlSocket.addEventListener(Event.CONNECT, function():void{trace('connect');});
_xmlSocket.addEventListener(IOErrorEvent.IO_ERROR, function():void{trace('io error')});
_xmlSocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function():void{trace('sec error')});
_xmlSocket.connect("127.0.0.1", 8888);
}
protected function button1_clickHandler(event:MouseEvent):void
{
_xmlSocket.send("client test");
}
protected function onData(event:DataEvent):void
{
trace(event.data);
}
]]>
</fx:Script>
<s:VGroup>
<s:TextInput id="ti" />
<s:Button label="test" click="button1_clickHandler(event)" />
</s:VGroup>
</s:WindowedApplication>
我的简单Node.js服务器:
const net = require('net');
var server = net.createServer(function(socket)
{
socket.on("data", function(message){
socket.write(message + "\n");
});
socket.on("connect", function(){ // Never triggered
console.log("connected to client\n");
});
socket.on("drain", function(){ // Never triggered
console.log("drain !\n");
});
socket.on("close", function(){
socket = undefined;
});
setInterval(function(){
if(socket)
{
socket.write("test\n");
}
}, 5000);
}).listen(8888, "127.0.0.1");
因此,当我点击我的Flex按钮时,数据从我的应用程序发送到我的服务器,从我的服务器发送到我的应用程序,所以它似乎很好。
当我等待5秒钟时,我的Flex没有收到&#34;测试&#34;字符串符合预期,但是当我在1分钟或更短时间内点击我的应用按钮后,我收到如下数据:
test
test
test
test
.....
client test
似乎我的服务器正在做一些&#34;保留信息&#34;但我可以解释为什么或为什么某些事件永远不会被触发(?)。
感谢您的帮助
答案 0 :(得分:0)
好的,我只是在发帖后找到解决方案(总是不是这样吗?)
我必须在消息中使用\0
。
socket.write("test\0");
不是
socket.write("test\n");