11 Replies Latest reply on Oct 18, 2010 11:37 AM by Claudiu Ursica

    Socket and keepalive

    Zolotoj Level 3

      How do I do keepalive with Flex sockets?

        • 1. Re: Socket and keepalive
          Claudiu Ursica Level 4

          Your server ask clients for Ping CommandMessage-s at regular time intervals.

          Whichever client fails to send the ping acknowledge gets disconnected by the

          server.

           

           

          C

          • 2. Re: Socket and keepalive
            Zolotoj Level 3

            How exactly my (Flex) server would ask that?

            • 3. Re: Socket and keepalive
              Claudiu Ursica Level 4

              There is no such thing as Flex server, maybe Haxe server if you insist using

              actionscript on server side. I am not sure what you have so far... share some

              requirements ... However you need to write the server side logic for thius

              listen for the socket connections on ports and make their management.

               

              C

              • 4. Re: Socket and keepalive
                Zolotoj Level 3

                <Your server ask clients for Ping CommandMessage-s at regular time intervals.

                <Whichever client fails to send the ping acknowledge gets disconnected by the

                <server.

                 

                If Flex connects to a socket on another computer which side is sending such commands?

                • 5. Re: Socket and keepalive
                  Claudiu Ursica Level 4

                  The server is managing the clients (flex or whatever language they are they

                  written in). Every time  a client connects the server knows it since it can

                  accept or deny the connection... So for every connected client the server needs

                  to know if they are still connected so It ask them to ping. Basically the

                  "asking them" par is the CommandMessage is just a name in communication

                  software, because the server commands the client to ping it. So the server

                  issues this commands at some regular intervals for all connected clients. Which

                  ever client fails to deliver the reply for the ping command gets disconnected.

                   

                   

                  If Flex connects to a socket on another computer which side is sending such

                  commands? So here the another computer is the server, since your flex client

                  connects to that computer. SO the programm running on the other compouter can be

                  java, c, air whatever issues the ping CommandMessages.

                   

                   

                  C

                  • 6. Re: Socket and keepalive
                    Zolotoj Level 3

                    I still dont understand.

                    I am listening in Flex to a socket on the network that nothing to do with any programming language. So, Who is pinging who?

                    • 7. Re: Socket and keepalive
                      Claudiu Ursica Level 4

                      Show me your code,

                      C

                      • 8. Re: Socket and keepalive
                        Zolotoj Level 3

                        private var socket:Socket = new Socket();

                        ...

                        socket.addEventListener(Event.CONNECT, onConnect);

                        socket.addEventListener(IOErrorEvent.IO_ERROR, onError);

                        socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

                         

                        socket.connect(ip, 8023);

                        ....

                        private function onConnect(event:Event):void

                        {

                             socket.flush();

                             socket.addEventListener(ProgressEvent.SOCKET_DATA, onData);

                        }

                         

                        • 9. Re: Socket and keepalive
                          Claudiu Ursica Level 4

                          Now this is client code. You should be having another piece of code that is

                          listening on the IP, and port 8023. When you call socket.connect(ip, 8023); you

                          connect to a machine running that piece of code typically called server. Tah

                          code aknowledge the fact that you called connect and will send some answer back

                          to you that you are connected. That answer will treigger your onConnect for

                          which you registered with this line socket.addEventListener(Event.CONNECT,

                          onConnect); Now you can send and recieve data.

                           

                          your onData will be called every time the application you connect to will send

                          something to you.

                           

                           

                          your onData should look something like this

                          private function onData(event:ProgressEvent):void

                                  {

                                      var data:ByteArray = readResponse();

                                      //do whatever you want with the bytes

                                  }

                           

                           

                          private function readResponse():ByteArray

                          {

                                      var byteArray:ByteArray = new ByteArray();

                                      socket.readBytes(byteArray, 0);   

                                      return byteArray;

                          }

                           

                           

                          Your server will call you to ping it. So insetad of these lines

                                      //do whatever you want with the bytes

                           

                          you should read the bytes and somehow figure out that you need to ping. Then

                          prepare a response message fluish the socket and wait.

                           

                          you should have a close handler also:

                           

                          socket.addEventListener(Event.CLOSE, closeHandler);

                           

                          private function closeHandler(event:Event):void

                                  {

                                      trace("closeHandler: " + event);

                                      //if this code e4xecutes you are disconnected.

                                  }

                           

                           

                          http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_5 .html

                           

                           

                          this also helps to get a better picture .

                           

                          C

                          • 10. Re: Socket and keepalive
                            Zolotoj Level 3

                            Like I said, I am not connecting to a socket created with any program. It is a piece of hardware that has an ip and a port.

                            • 11. Re: Socket and keepalive
                              Claudiu Ursica Level 4

                              I believe than that piece of hardware needs to come with a communication

                              protocol. And that piece of hardware acts like the server. You should have the

                              protocol specs and also what commands does that piece of hardware accepts. If it

                              doesn't ask by itself for the keepalive, maybe it has some api's exposed to

                              you...

                               

                              C