package { import flash.display.Sprite; import flash.events.*; import flash.net.Socket; publicclass SocketExample extends Sprite { privatevar socket:Socket; publicfunction SocketExample( ) { socket = new Socket( ); // Add an event listener to be notified when the connection // is made socket.addEventListener( Event.CONNECT, onConnect ); // Connect to the server socket.connect( "localhost", 2900 ); } privatefunction onConnect( event:Event ):void { trace( "The socket is now connected..." ); } } }
如果使用XMLSocket,代码也基本上相同,代码如下:
+展开
-ActionScript
package { import flash.display.Sprite; import flash.events.*; import flash.net.XMLSocket; publicclass SocketExample extends Sprite { privatevar socket:XMLSocket; publicfunction SocketExample( ) { socket = new XMLSocket( ); // Add an event listener to be notified when the connection is made socket.addEventListener( Event.CONNECT, onConnect ); // Connect to the server socket.connect( "localhost", 2900 ); } privatefunction onConnect( event:Event ):void { trace( "The xml socket is now connected..." ); } } }