AS3需要在每条消息中打开一个新套接字吗?

时间:2016-09-01 04:30:36

标签: javascript node.js sockets actionscript-3 flash

我在nodeJS服务器和Flash套接字客户端之间进行了连接。

当我打开客户端时,与服务器的连接是成功的,但我只能在此套接字连接上发送一条消息。如果我想发送更多消息,我需要为每条消息打开另一个连接。如果我不打开这个新的连接,套接字不会发送更多数据。

注意:我在flashdevelop工作

问题:

这是从客户端套接字向服务器发送数据的常规方法吗?如果不是这样,你能告诉我如何只用一个连接插座发送更多的消息吗?

我的服务器代码:



try {
    Security.allowDomain('xxx.xxx.x.xx');
    Security.loadPolicyFile("xmlsocket://xxx.xxx.x.xx:3000");
} catch (e) {

}

var net = require('net');


var server = net.createServer(function(stream) {
    stream.addListener("connect", function() {
        console.log("Client connected");
        stream.write("hello\r\n");
    });

    stream.addListener("data", function(data) {
        console.log(stream);
        console.log("Received from client: " + data);
        stream.write(data);
    });

    stream.addListener("end", function() {
        console.log("Client disconnected");
        stream.end();
    });
});

server.listen(3000, "xxx.xxx.x.xx");
console.log("running");




我的AS3客户端代码:



package ejempo1
{    import flash.display.MovieClip;
     import flash.events.Event;
     import flash.events.MouseEvent;
     import flash.events.KeyboardEvent;
     import flash.external.ExternalInterface;
     import flash.text.*;
     import flash.utils.*;

     import flash.display.Sprite;
     import flash.events.ProgressEvent;
     import flash.net.Socket;

     public class Main extends MovieClip
    {
        private var socket:Socket;

        public function Main()
        {

            stage.addEventListener(MouseEvent.CLICK, clickHandler);


            //socket de conexion
            socket = new Socket();
            
            socket.connect("xxx.xxx.x.xx", 3000);
            socket.writeUTFBytes("Hello server");//this message work fine

            socket.addEventListener(Event.CONNECT, connectHandler);
            socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);

        }
        
        private function connectHandler(e:Event):void {
            trace(e);
            trace("connect");
        }
        
        private var toRead:int = 0;
        
        private function socketDataHandler(e:ProgressEvent):void {
            trace("socketDataHandler: " + e);
            var str:String = socket.readUTFBytes(e.bytesLoaded);
            trace("server say:"+str);
            
        }


        public function send(data:Object):void {
            socket.writeObject(data);
            socket.flush();
        }
        
        
        private function clickHandler(event:MouseEvent):void {
                socket.connect("xxx.xxx.x.xx", 3000);//if I delete this line, the next code line dont work when I make a click.
                socket.writeUTFBytes("pepe");
             
        } 
    }
}




0 个答案:

没有答案