Package helper. socket {import config. configvar; import flash. errors. ioerror; import flash. events. event; import flash. events. eventdispatcher; import flash. events. ioerrorevent; import flash. events. progressevent; import flash. events. securityerrorevent; import flash.net. socket; import helper. hh_fun; public class phpsocket extends eventdispatcher {private var msocket: socket; protected var mdone: Boolean = true; PRI Vate var maddr: string; private var mport: int; private VaR _ typekey: string; Public Function phpsocket (typekey: string) {connmoresocket (typekey );} /** connect multiple socket servers */private function connmoresocket (typekey: string): void {// common socket communication _ typekey = typekeyif (typekey = "Comm ") {startconnection ("192.168.1.222", 8080); trace ("common socket communication address:" + configvar. commsocketiparr [0] + "port:" + configvar. commsocketiparr [1]) ;} // Chat system socket communication if (typekey = "chat") {startconnection ("192.168.1.333", 8000); trace ("the socket communication address of the chat system is: "+ configvar. chatsocketiparr [0] + "port:" + configvar. chatsocketiparr [1]) ;}}/** common socket connection */Private Static Var instance: phpsocket; public static function Init (): phpsocket {If (instance = NULL) {instance = new phpsocket ("Comm");} return instance;}/** chat system socket connection */Private Static Var chatinstanceval: PHPs Ocket; public static function chatinstance (): phpsocket {If (chatinstanceval = NULL) {chatinstanceval = new phpsocket ("chat");} return chatinstanceval ;} // begin ----------/** start link */Public Function startconnection (ADDR: String, Port: INT): void {maddr = ADDR; mport = port; mdone = False; msocket = new socket (); msocket. addeventlistener (event. connect, notifyconnected); // after the connection is successful, first send a message msocket. addeventlistener (event. close, notifydisconnected); msocket. addeventlistener (ioerrorevent. io_error, ioerrorhandler); msocket. addeventlistener (securityerrorevent. security_error, securityerrorhandler); msocket. addeventlistener (progressevent. socket_data, socketdatahandler); try {msocket. connect (Maddr, mport);} catch (E: securityerror) {trace (E. getstacktrace () ;}}/** send data to the socket server */Public Function sendpackettoserver (MSG: String, keystr: String, conntime: Int = 0 ): void {If (msocket & msocket. connected) {try {msocket. writeunsignedint (hh_fun.instance (). getstringlength (MSG. tostring (); msocket. writeutfbytes (MSG); trace ("successfully sent to the socket server:" + MSG);} catch (E: ioerror) {trace ("failed to send the client to the server: "+ E. gets Tacktrace () ;}} else {}}/** receives data from the socket server in segments */private var ss_isreadstr: Boolean = false; private var ss_msglength: int = 0; private var ss_readedmsg: String = ""; Private function socketdatahandler (Event: progressevent): void {trace ("the server has information sent to the client "); // determine whether to read new data or read old data. If (ss_isreadstr = false) {readhaveheader ();} // read only the string else {readfun () ;}/ ** read socket information header */private function readhaveheader (): void {s S_msglength = msocket. readint (); ss_isreadstr = true; readfun () ;}/ ** read the message function. When this function is executed, it has read the information header */private function readfun (): void {var thismsglength: Int = msocket. bytesavailable; If (thismsglength> 0) {// read the serialized data packet var readlength: Int = (thismsglength >= ss_msglength )? Ss_msglength: thismsglength; ss_readedmsg + = msocket. readutfbytes (readlength); ss_msglength-= readlength; // If (ss_msglength = 0) after reading is complete {// stoc (ss_readedmsg) after reading is complete; ss_isreadstr = false; ss_readedmsg = ""; ss_msglength = 0; // if there is any remaining information, continue reading the header if (msocket. bytesavailable> 0) {readhaveheader () ;}}/ ** simulate the server sending information to the client */Public Function stoc (STR: string): void {var E: eesocket = new eesocket (eesocket. socket); E. MSG = STR; dispa Tchevent (E);} // error when connecting to the socket Protected function policyconnected (Event: Event): void {trace ("connected to the server !! "); // Sendpackettoserver (" <policy-file-Request/> ");} protected function policydisconnected (EVT: Event): void {trace (" server disconnected! ");} Private function ioerrorhandler (Event: ioerrorevent): void {trace (" header Link error ioerrorhandler: "+" port: "+ mport +" "+ event. text);} private function securityerrorhandler (Event: securityerrorevent): void {trace ("sandbox error securityerrorhandler:" + "port:" + mport + "" + event. text );}}}