flex和delphi做的服务器进行通讯
2020-03-01 06:11
417 查看
用flex做前台页面,后台使用delphi处理数据,之间用socket连接。。。delphi使用indy的tcpserver。的确很好玩。哈哈。可以不知道那些网页游戏是否是这样做成的。。。先贴个效果图,再贴代码。。很简单的说。
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdUDPBase, IdUDPServer, IdBaseComponent, IdComponent,
IdTCPServer;
type
TfFlexServerTest = class(TForm)
Memo1: TMemo;
Button1: TButton;
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure log(logInfo:string);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fFlexServerTest: TfFlexServerTest;
implementation
{$R *.dfm}
procedure TfFlexServerTest.log(logInfo:string);
begin
Memo1.Lines.Add(logInfo);
end;
procedure TfFlexServerTest.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
log('有人来连接');
end;
procedure TfFlexServerTest.IdTCPServer1Execute(AThread: TIdPeerThread);
var
s:string;
begin
s:=AThread.Connection.ReadLn;
s:=UTF8Decode(s);
log(s);
end;
procedure TfFlexServerTest.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
log('有人离开');
end;
end.
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#000000, #FFFFFF]" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import flash.net.Socket;
import flash.events.MouseEvent;
private var YTsocket:Socket=new Socket();
internal function initApp():void
{
YTsocket.connect("127.0.0.1",1985);
btn1.addEventListener(MouseEvent.CLICK,onClick);
}
internal function onClick(event:MouseEvent):void
{
var msg:ByteArray=new ByteArray();
msg.writeUTFBytes(txt.text+"\n");
//YTsocket.writeBytes(msg);
YTsocket.writeUTFBytes(msg+"\n");
YTsocket.flush();
}
]]>
</mx:Script>
<mx:Panel width="250" height="200" layout="absolute" title="消息发送方" fontSize="15" backgroundAlpha="0.15" alpha="1.0" horizontalCenter="-11" verticalCenter="5">
<mx:Button x="82" y="111" label="发送" id="btn1" fontSize="13"/>
<mx:TextArea height="72" id="txt" horizontalCenter="0" verticalCenter="-31" color="#FA040A"/>
</mx:Panel>
</mx:Application>
服务端代码:
unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdUDPBase, IdUDPServer, IdBaseComponent, IdComponent,
IdTCPServer;
type
TfFlexServerTest = class(TForm)
Memo1: TMemo;
Button1: TButton;
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
procedure log(logInfo:string);
private
{ Private declarations }
public
{ Public declarations }
end;
var
fFlexServerTest: TfFlexServerTest;
implementation
{$R *.dfm}
procedure TfFlexServerTest.log(logInfo:string);
begin
Memo1.Lines.Add(logInfo);
end;
procedure TfFlexServerTest.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
log('有人来连接');
end;
procedure TfFlexServerTest.IdTCPServer1Execute(AThread: TIdPeerThread);
var
s:string;
begin
s:=AThread.Connection.ReadLn;
s:=UTF8Decode(s);
log(s);
end;
procedure TfFlexServerTest.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
log('有人离开');
end;
end.
客户端代码:
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#000000, #FFFFFF]" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import flash.net.Socket;
import flash.events.MouseEvent;
private var YTsocket:Socket=new Socket();
internal function initApp():void
{
YTsocket.connect("127.0.0.1",1985);
btn1.addEventListener(MouseEvent.CLICK,onClick);
}
internal function onClick(event:MouseEvent):void
{
var msg:ByteArray=new ByteArray();
msg.writeUTFBytes(txt.text+"\n");
//YTsocket.writeBytes(msg);
YTsocket.writeUTFBytes(msg+"\n");
YTsocket.flush();
}
]]>
</mx:Script>
<mx:Panel width="250" height="200" layout="absolute" title="消息发送方" fontSize="15" backgroundAlpha="0.15" alpha="1.0" horizontalCenter="-11" verticalCenter="5">
<mx:Button x="82" y="111" label="发送" id="btn1" fontSize="13"/>
<mx:TextArea height="72" id="txt" horizontalCenter="0" verticalCenter="-31" color="#FA040A"/>
</mx:Panel>
</mx:Application>
转载于:https://www.cnblogs.com/OneSpeak/articles/1527295.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Unity C#作为客户端与C++服务器 传递结构体进行Socket通讯
- Flex与Asp.net通过Remoting方式进行通讯
- [原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (三)
- 安卓客户端通过socket与服务器进行通讯
- SSE:服务器发送事件,使用长链接进行通讯 – PointNet – 博客园
- 用Delphi建立通讯与数据交换服务器—Transceiver技术剖析(下)
- 服务器如何跟多个客户端进行通讯呢?
- 原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (一)
- 用Delphi建立通讯与数据交换服务器—Transceiver技术剖析(上)
- Flex与采用Https协议的服务器通讯
- 使用hessian进行服务器与客户端通讯
- 地图入门(二):和服务器进行通讯,在地图上做标注
- [原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (二)
- 推荐几个Adobe Flex Builder 3的插件(代码格式化和fms服务器通讯文件(main.asc)编写)
- 用Delphi建立通讯与数据交换服务器—Transceiver技术剖析(上)
- Flex3使用Httpservice与服务器进行异步通讯
- Delphi中如何进行HTTP编程,比如:发送一个XML文件给服务器?
- [原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (三)
- SSE:服务器发送事件,使用长链接进行通讯
- 用Delphi建立通讯与数据交换服务器—Transceiver技术剖析(下)