有人可以指点我为 netyy 服务器制作一个 flash 二进制套接字客户端吗?
问问题
564 次
1 回答
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);
一个连接netty服务器的函数
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 游戏服务器执行登录功能的函数。您可以忽略登录等,只是试图展示它如何与 netty 一起使用。要查看实际的java 游戏服务器代码,请转到提供的 github 链接。
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");
}
}
于 2012-04-21T03:48:54.937 回答