AMF data package using the Socket communication with Flash

2010-11-08  来源:本站原创  分类:Flash  人气:146 

AS3's Socket class provides us with a method and writeObject readObject method and server communication allows us to readability and transmission efficiency in the agreement the two into account. Early in the network game development if you are not sure you fully planned agreement, then the use of this method is a good choice. Until all the debugging smooth, and then based on this type of event abstraction.

The following open source projects we use Adobe BlazeDS AMF3 classes provided in package with Flash Socket Communications introduce this approach.
See text and presentation: http://www.iflashigame.com

Server-side code AmfServer.java. (Based on code reform Weni)

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Input;
import flex.messaging.io.amf.Amf3Output;
public class AmfServer
{
public static void main(String args[])
{
   SerializationContext serializationContext=new SerializationContext();  

   //  Object serialization amf3
   Amf3Output amfout=new Amf3Output(serializationContext);  

   //  To achieve an output stream, which data is written to a   byte   Array  .
   ByteArrayOutputStream byteoutStream=new ByteArrayOutputStream();  

   //  Flow of the resulting array will byteoutStream into  DataOutputStream  Stream
   DataOutputStream dataoutstream=new DataOutputStream(byteoutStream);  

   //  Create ServerSocket and  Socket  Object
   ServerSocket serverSocekt;
   Socket socket;  

   //   Stream encoding format is set  amf3
   amfout.setOutputStream(dataoutstream);  

   //  Creating Map Objects  .Double  Array of objects
   HashMap map=new HashMap();
   map.put("Event", "  People moves  ");
   map.put("user", "  Flash knife prodigal son  ");
   map.put("x", 100);
   map.put("y", 100);    

   try {
    amfout.writeObject(map);//  The object is actually written to the map  dataoutstream  Stream
    dataoutstream.flush();//  Empty the cache
   } catch (IOException e) {
    e.printStackTrace();
   }  

   //  Will transit into a byte array ByteArrayOutputStream stream
   byte[] messageBytes=byteoutStream.toByteArray();//amf3  Data   

   OutputStreamWriter osw;// Amf3 format will be written using the data stream into a byte code
   BufferedWriter bwrite;//  Used to encapsulate OutputStreamWriter, to improve efficiency    

   try {  

    System.out.println("  Output array length  "+messageBytes.length);
    serverSocekt=new ServerSocket(1028);//  Open the server process
    System.out.println("  Server has been started  .......");
    socket=serverSocekt.accept();
    if(socket.isConnected())
    {
     System.out.println(">>>>>>>>>>  The client is connected  ");
    }
    //socket.
    osw=new OutputStreamWriter(socket.getOutputStream());//  Flow into a byte stream of characters
    bwrite=new BufferedWriter(osw);//  Package osw object  ,  Improve the efficiency of writing    

    socket.getOutputStream().write(messageBytes);//  Write binary data to the stream
    socket.getOutputStream().flush();
    socket.getOutputStream().close();  

   } catch (FileNotFoundException e) {
    e.printStackTrace();
   }
   catch (IOException e) {
    e.printStackTrace();
   }
}
}

the example above, we simulate an event - people move, and the relevant data to be encapsulated inside an Object object.
Here is the client code (AmfSocket.fla), because there is no simple random annotated.

var socket:Socket=new Socket();
socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler);
socket.connect("localhost",1028);
var obj:Object=new Object();
addEventListener("recieved",recievedData);
function recievedData(evt:Event)
{
switch (obj.Event) {
   case "  People moves  " :
    trace("  The players  :"+obj.user+"   Move to  "+obj.x+","+obj.y);
    break;
   case "  People Dress  " :
    trace("  People Dress  ");
    break;
   case "  Message Broadcast  " :
    trace("  System Information  ");
    break;
   default :
    break;
}
};
function closeHandler(event:Event):void {
trace("  Connection closed  ");
}
function connectHandler(event:Event):void {
trace("  Successful connection  ");
}
function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler  Information  : " + event);
}
function securityErrorHandler(event:SecurityErrorEvent):void {
trace("securityErrorHandler  Information  : " + event);
}
function socketDataHandler(event:ProgressEvent):void {
trace("  Receive data  ");
obj=socket.readObject();
this.dispatchEvent(new Event("recieved"));
}

execution results:
Successful connection to receive data flash prodigal son to move to the 100,100 knives
Connection closed

相关文章
  • AMF data package using the Socket communication with Flash 2010-11-08

    AS3's Socket class provides us with a method and writeObject readObject method and server communication allows us to readability and transmission efficiency in the agreement the two into account. Early in the network game development if you are not s

  • flex as3 the socket communication with java 2010-11-02

    Today whim, web flash games are how to do, so I looked at flash and java for socket communication, the results really easy to use. . . Too evil New flash file: SocketExample.fla Add button: btnSend Modify the document class: SocketExample New AS3 fil

  • flex with the SOCKET Communication JAVA 2010-04-28

    First, to prepare: Server: JDK1.5 (not described in this bar?) Server-side IDE: eclipse (its home page) Client: FLEX 3 (Adobe ® Flex ® 3 is used to build and maintain in all major browsers , desktops and operating systems consistently deployed Web ap

  • Flash with php's socket with the socket communication 2010-07-14

    Flash with php the socket with the socket communication Not much said on the socket, that a lot of information online, information on the flash and there are many here that only the Internet can not find the information or data of Oh little things ph

  • UDP-based socket communication java 2010-09-03

    1. Socket communication are two kinds, one is based on a reliable tcp connection, a UDP-based connectionless packet delivery service connection, UDP communication common to the class are: DatagramSocket class and the DatagramPacket class. 2. Datagram

  • flex socket communication problems encountered with the java 2010-11-25

    In fact, stick bag problem. Honestly, until now still not understood the underlying principle is triggered SOCKET_DATA, speculation as to whether the polling mechanisms, who fully understand, but please feel free. Anyway, the current situation encoun

  • There's TCP Socket Communication Interface 2010-04-19

    There is a very simple interface for TCP Socket communication procedures Program composed of four categories: one is server-side; one client; the other two classes, used to send messages and receive messages. Server-side TcpServer.java package TcpCha

  • [Tutorial] AS 3.0 socket communication, the whole basis of comparison compare, do not go around the 2010-08-27

    Transfer from http://520pig.blogbus.com/logs/32821429.html Socket socket connection allows Flash player by specifying the port communicate with the server, but the socket after the data transfer communication is still not close to artificially close.

  • android socket communication 2010-10-25

    android socket communication As a fully open source Android mobile platform, has been a lot of developers the most potential as a future smart phone operating system. And the short period of time has been a large number of third-party applications ap

  • ORA-17410: No more data to read from socket 2010-05-11

    JAVA Application Layer Spring + iBatis + Proxool access ORACLE 11 RAC There will always be a few errors every day ORA-17410: No more data to read from socket Finding out the reasons

  • (Help) J2ME mobile client and the PC's socket communication 2010-08-04

    Much to ask you, how to use the J2ME phone and PC to achieve the Socket Communication? I want a cell phone and cell phone chat between the applet, use the socket connection, the phone connected to the PC side of the server, but the PC side of the ser

  • java SSL secure socket communication instance 2010-10-13

    1. .. Use keytool to create key pair and server and the client to complete the specific method of allocation can be found ..: java-use keytool to create key and certificate management, etc.-java study notes (3) 2. To create client and server socket e

  • Java and C + + communication attempt Socket communication 2010-05-07

    Most of the code must come from http://www.z6688.com/info/37437-1.htm , but in their own process of debugging done something to add. Java and C + + complex to use other means (such as: JNI's), today tried a Socket specifically means: This code shows,

  • The need for large data package to send in multiple MDL 2010-03-27

    A while ago, we have resolved how to send custom network data. Then the next thing is, if you want to send the data much, the need to split the data, and then package it in a number of MDL to send? As NdisAllocateMDL function can only allocate a MDL,

  • php socket communication basics 2010-09-25

    Intriguing and easily confused people Socket (Sockets). Sockets in PHP is not fully utilized features. Today you will see create a connection to the server using the client and the client to connect using the socket, the server side will handle the d

  • java socket communication 2010-09-27

    Write two programs: 1.ManyToOne: a multiple client server multi-threaded, client-side one-way message to the server. 2.OneToOne: a client and a server, two-way communication, reading and writing are different threads on. (Both sides need to enter the

  • POST data package with SAFEARRAY 2010-11-26

    Package using the SAFEARRAY data POST. SAFEARRAY * psa; // Define safearray SAFEARRAYBOUND rgsabound[1]; // According to the dimension of the array to define several array element, this example is a single-dimensional array rgsabound[0].cElements = p

  • C socket communication (Linux + window) 2010-12-30

    Recent examples of some of my friends refer to the Internet, combined with his understanding of the development of linuxC, made a simple linux system C, socket programming examples of communication, as well as window socket communications under the C

  • Bank side of the socket communication receipt issued by the length of the high and low order header. 2011-09-21

    First, describe the scene, to do with java server in the case, to obtain the correct header (ie, packet length), I was listening to a more experienced colleague's question, said the case of banks with C may be obtained The length (int or short or lon

  • c socket communication with php udp 2010-04-05

    C language for server-side background, foreground php with the client. 1. Background c language code: (1) socket_connect.h #ifndef SOCKET_CONNECT_H #define SOCKET_CONNECT_H #include <sys/types.h> #include <sys/socket.h> #include <string.h&g