Home » Flash相关 » 页游C++登陆器 » C++微端与As3网页游戏之间交互

C++微端与As3网页游戏之间交互

新版的微端,我决定新增一些特性:比如关闭会有引导,提示下次进入游戏入口。比如游戏swf和微端之间可以进行通讯。
之前的思路是:C++与JavaScript进行通讯,AS3与JavaScript通讯,然后C++通过JavaScript可以和AS3进行交互。但是有一个局限性,那就是
如果SWF文件如果被嵌在一个Frame标签里,那么JS就会遇到脚本跨域的问题。因为COM的安全限制,C++与AS3之间的通讯就是受到了限制。
那么新的思路是:直接通过TCP连接C++和AS3。
C++这边写个Socket服务器,AS3充当客户端连接。TCPServer 我们用的indy系列的代码。使用的是Delphi写的,但是可以在C++里面使用。
以下是核心代码:

服务器端代码:

//—————————————————————————
void __fastcall TForm4::Button1Click(TObject *Sender)
{
here1:
try
{
this->IdTCPServer1->Bindings->Clear();
TIdSocketHandle * ha = this->IdTCPServer1->Bindings->Add();
ha->IP = “127.0.0.1”;
ha->Port = serverPort ;
this->IdTCPServer1->Active = true;
}
catch(…)
{
serverPort++;
goto here1;
}

this->Memo1->Lines->Add(“启动服务器” + IntToStr(serverPort));
}
//—————————————————————————

void __fastcall TForm4::Button2Click(TObject *Sender)
{
TList * list = this->IdTCPServer1->Contexts->LockList();

for(int i = 0 ; i < list->Count ; i++)
{
TIdContext * AContext = (TIdContext*) list->Items[0];
AContext->Connection->IOHandler->WriteBufferClear();
AContext->Connection->IOHandler->InputBuffer->Clear();
AContext->Connection->IOHandler->Close();
if(AContext->Connection->Connected())
{
AContext->Connection->Disconnect();
}
}
this->IdTCPServer1->Contexts->UnlockList();
if(this->IdTCPServer1->Active)
{
this->IdTCPServer1->Active = false;
}
this->Memo1->Lines->Add(“关闭服务器”);
}

//—————————————————————————
void __fastcall TForm4::IdTCPServer1Execute(TIdContext *AContext)
{
int len = AContext->Connection->IOHandler->InputBuffer->Size;
if(len==0)
{
return;
}
if(!this->IdTCPServer1->Active)
{
AContext->Connection->Disconnect();
return;
}
TBytes byte ;
TBytes byte3;
AContext->Connection->IOHandler->ReadBytes(byte , len ,false) ;
// UnicodeString str = AContext->Connection->Socket->ReadLn() ;
UnicodeString str = StringOf(byte);
UnicodeString cross = L”<policy-file-request/>” ;
str = str.Trim() ;
if(str == cross)
{

UnicodeString result ;
result = “<?xml version=”1.0” ?> <cross-domain-policy> <allow-access-from domain=”*” to-ports=”*”/> </cross-domain-policy>” ;
TBytes byte2 = BytesOf(result );
AContext->Connection->IOHandler->Write(byte2) ;
AContext->Connection->IOHandler->WriteBufferFlush();
AContext->Connection->IOHandler->Close(); //必须要主动关闭连接,flash客户端会自动发起重连请求的

hasBeenConected = true;

this->Memo1->Lines->Add(str);
this->Memo1->Lines->Add(result);
}
else
{
str = WideStringOf(byte);
if(str == L”checkn”)
{
TList * list = this->IdTCPServer1->Contexts->LockList();
str = L”count:”+ IntToStr(list->Count);
byte3 = BytesOf(str );
AContext->Connection->IOHandler->Write(byte3) ;
AContext->Connection->IOHandler->WriteBufferFlush();
}
else
{
str = “服务器:”+str;
byte3 = BytesOf(str );
AContext->Connection->IOHandler->Write(byte3) ;
AContext->Connection->IOHandler->WriteBufferFlush();
}
this->Memo1->Lines->Add(str);
}
}
//—————————————————————————
void __fastcall TForm4::IdTCPServer1Connect(TIdContext *AContext)
{
if(this->Memo1 != NULL)
{
TDateTime remoteTime = Now();
this->Memo1->Lines->Add(“连接成功:”+remoteTime.DateTimeString());
}

}
//—————————————————————————

void __fastcall TForm4::IdTCPServer1Disconnect(TIdContext *AContext)
{
if(this->Memo1 != NULL)
{
TDateTime remoteTime = Now();
this->Memo1->Lines->Add(“连接断开 :” +remoteTime.DateTimeString());
}
}
//—————————————————————————
void __fastcall TForm4::IdTCPServer1Exception(TIdContext *AContext, Exception *AException)

{
if(this->Memo1 != NULL)
{
this->Memo1->Lines->Add(AException->ToString()) ;
}
}
//—————————————————————————

void __fastcall TForm4::IdTCPServer1ListenException(TIdListenerThread *AThread, Exception *AException)

{
if(this->Memo1 != NULL)
{
this->Memo1->Lines->Add(AException->ToString()) ;
}
}
//—————————————————————————
void __fastcall TForm4::Button3Click(TObject *Sender)
{
TList * list = this->IdTCPServer1->Contexts->LockList();

for(int i = 0 ; i < list->Count ; i++)
{
TIdContext * AContext = (TIdContext*) list->Items[0];
AContext->Connection->IOHandler->Write( BytesOf(this->Edit1->Text));
AContext->Connection->IOHandler->WriteBufferFlush();
}
}

客户端代码

protected function button1_clickHandler(event:MouseEvent):void
{
sock.addEventListener(Event.CONNECT,onConnect);
sock.connect(“127.0.0.1″,10803);
sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR , onSecurityError);
sock.addEventListener(ProgressEvent.SOCKET_DATA,onReceiveData);
sock.addEventListener(Event.CLOSE ,onClose);
}

protected function onConnect(event:Event):void
{
trace(“连接上”);
textA.text += “连接成功n” ;
}

protected function onSecurityError(event:SecurityErrorEvent):void
{
trace(“安全沙箱错误”);
Alert.show(“安全沙箱错误”+event.toString());
}

protected function button2_clickHandler(event:MouseEvent):void
{
var str:String = this.input.text;

var byte:ByteArray = new ByteArray();
byte.writeMultiByte(str+”n”,”unicode”);
// sock.writeByte(byte.length);
sock.writeBytes(byte,0,byte.length);
sock.flush();
}

private var data:ByteArray = new ByteArray();
protected function onReceiveData(event:ProgressEvent):void
{
data.length = 0;
sock.readBytes(data,0 , sock.bytesAvailable);
var str:String = data.readMultiByte(data.bytesAvailable,”ansi”);
textA.text += str+”n” ;
}

protected function onClose(event:Event):void
{
// TODO Auto-generated method stub
textA.text += “连接断开n” ;
}

protected function button3_clickHandler(event:MouseEvent):void
{
this.sock.close();
}

    分享到: