与netty的Flash二进制套接字通信

时间:2012-04-20 18:36:53

标签: flash sockets binary netty

对于netyy服务器,有人可以指点我制作一个flash二进制套接字客户端吗?

1 个答案:

答案 0 :(得分:0)

你可以在网上找到很多这样的例子,无论如何都要在这里粘贴一些代码花絮。

import flash.net.Socket;
private var remoteHost:String;
private var portNumber:int;

// Create a new Socket object and assign event listeners.
socket = new Socket();
socket.addEventListener(Event.CONNECT, connectHandler);
socket.addEventListener(Event.CLOSE, closeHandler);
socket.addEventListener(ErrorEvent.ERROR, errorHandler);
socket.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

连接到网络服务器的功能

public function connect():Boolean
    {
        // Load policy file from remote server.
        //Security.loadPolicyFile("xmlsocket://" + remoteHost + ":" + policyPort);
        // Attempt to connect to remote socket server.
        try {
            trace("Trying to connect to " + remoteHost + ":" + portNumber + "\n");
            socket.connect(remoteHost, portNumber);
        } catch (error:Error) {
            /*
                Unable to connect to remote server, display error 
                message and close connection.
            */
            trace(error.message + "\n");
            socket.close();
            return false;
        }
        return true;
    }

为我创建的网络游戏服务器执行登录功能的功能。您可以忽略登录等,只是试图展示它如何与netty一起使用。要查看实际提供的java game server code goto github link

    private function connectHandler(event:Event):void {
        if (socket.connected) {
            socket.addEventListener(ProgressEvent.SOCKET_DATA, loginHandler);
                    var payLoad:ByteArray = new ByteArray();
            payLoad.writeByte(LOG_IN);//opcode sent back to netty server in my implementation
            payLoad.writeBytes(encodeMultiStrings(user, pass, roomName));// credentials
            trace("Payload length before encode is: " + payLoad.length);
            var login:ByteArray = encodeBytes(payLoad);
            trace ("Login bytes length: " + login.length);
            writeBytesToSocket(login);
        }else {
            trace("unable to connect\n");
        }
    }