简体   繁体   中英

Testing Node.js server and Flash socket

I'm trying to establish socket connexion between a Node.js server and a Flash (Flex) application. I have some unexpected results that I can't explain by myself.

Here are my code.

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>

My simple Node.js server :

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");

So when I click on my Flex button, data is sending from my app to my server and from my server to my app so it seems to be good.

When I waiting for 5 seconds my Flex does not receive "test" String as expected but when I click on my app button after 1 minute or less I receive data like this :

test  
test  
test  
test  
.....  
client test

It seems that my server doing some "retention of information" but I can explain why or why some events are never triggered as it should be (?).

Thanks for help

Ok I'm just finding solution after posting (it's always the case no ?)

I have to use \\0 in message.

socket.write("test\0");

NOT

socket.write("test\n");

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