Created
March 25, 2015 15:54
-
-
Save zouloux/4d054b0661d2212aee2a to your computer and use it in GitHub Desktop.
How to send correctly data from Socket with Adobe Air
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Donnée sur les socket | |
| */ | |
| protected function socketDataHandler (event:ProgressEvent):void | |
| { | |
| // Cibler le socket | |
| var socket:Socket = (event.target as Socket); | |
| // Here we get our file on ByteArray | |
| contentBytes = ... | |
| // On retourne un 200 et les données du fichier | |
| socket.writeUTFBytes("HTTP/1.1 200 OK\n"); | |
| socket.writeUTFBytes("Content-Type: " + getMimeType(filePath) + "\n"); | |
| socket.writeUTFBytes("Content-Length: " + contentBytes.length + "\n"); | |
| // Ajouter le contenu | |
| socket.writeUTFBytes("\n"); | |
| socket.writeBytes(contentBytes); | |
| // Ne plus écouter les connexions sur ce socket | |
| socket.removeEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); | |
| // Ecouter la progression du transfert vers le client | |
| socket.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, socketOutputProgressHandler); | |
| // On vide le socket et on le ferme | |
| socket.flush(); | |
| } | |
| /** | |
| * Progression du transfert vers le client | |
| */ | |
| protected function socketOutputProgressHandler (event:OutputProgressEvent):void | |
| { | |
| // Cibler le socket | |
| var socket:Socket = (event.target as Socket); | |
| // Si on n'a plus de données à transférer | |
| if (socket.bytesPending == 0) | |
| { | |
| // Supprimer l'écoute | |
| socket.removeEventListener(OutputProgressEvent.OUTPUT_PROGRESS, socketOutputProgressHandler); | |
| // Fermer la connexion | |
| socket.close(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment