扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
IChatManager = interface(IDispatch) ['{E7CD7F0D-447F-497A-8C7B-1D80E748B67F}'] procedure SpeakTo(const content: WideString; destid: Integer); safecall; //客户向指定的房间说话,destid为房间号 function ReadFrom(sourceid: Integer): IStrings; safecall; //客户从指定的房间读取谈话内容,sourceid为房间号 function ReadReady(id: Integer): Byte; safecall; //客户检测指定的房间是否已经可以读取谈话内容 procedure ConnectRoom(const UserName: WideString; RoomID: Integer); safecall; //客户登陆指定房间 procedure DisconnectRoom(const UserName: WideString; RoomID: Integer); safecall; //客户退出指定房间 function TestClearBufferTag(RoomID: Integer): Integer; safecall; //客户测试指定房间的缓冲区的清空与否状况 end; 再来看看接口的实现类TChatManager部分: type TChatManager = class(TAutoObject, IChatManager) protected function ReadFrom(sourceid: Integer): IStrings; safecall; //在这里我们使用Delphi扩展的复杂类型TStings,为了让COM支持这种 //类型,delphi提供了IStrings接口 procedure SpeakTo(const content: WideString; destid: Integer); safecall; function ReadReady(id: Integer): Byte; safecall; //用来提供给客户端查询指定的房间是否可读,既指定房间缓冲区是否为空 procedure ConnectRoom(const UserName: WideString; RoomID: Integer); safecall; procedure DisconnectRoom(const UserName: WideString; RoomID: Integer); safecall; function TestClearBufferTag(RoomID: Integer): Integer; safecall; end; |
function TChatManager.ReadFrom(sourceid: Integer): IStrings; var TempRoom:TChatRoom; begin TempRoom:=ChatRoomManager.FindRoomByID(sourceid); while TempRoom.Locked do begin //do nothing只是等待解锁 end; GetOleStrings(TempRoom.OneRead,Result); end; procedure TChatManager.SpeakTo(const content: WideString; destid: Integer); var TempRoom:TChatRoom; begin TempRoom:=ChatRoomManager.FindRoomByID(destid); while TempRoom.Locked do begin //do nothing只是等待解锁 end; TempRoom.OneSpeak(content); end; function TChatManager.ReadReady(id: Integer): Byte; var TempRoom:TChatRoom; begin TempRoom:=ChatRoomManager.FindRoomByID(id); if TempRoom.CanRead then result:=1 else Result:=0; end; procedure TChatManager.ConnectRoom(const UserName: WideString; RoomID: Integer); //客户端通过接口登陆到指定的房间,没有完全实现 var TempRoom:TChatRoom; begin TempRoom:=ChatRoomManager.FindRoomByID(RoomID); TempRoom.LoginRoom(UserName); end; procedure TChatManager.DisconnectRoom(const UserName: WideString; RoomID: Integer); //客户端通过接口离开指定的房间,没有完全实现 var TempRoom:TChatRoom; begin TempRoom:=ChatRoomManager.FindRoomByID(RoomID); TempRoom.LeaveRoom(UserName); end; function TChatManager.TestClearBufferTag(RoomID: Integer): Integer; var TempRoom:TChatRoom; begin TempRoom:=ChatRoomManager.FindRoomByID(RoomID); result:=TempRoom.ClearBufferTag; end; initialization TAutoObjectFactory.Create(ComServer, TChatManager, Class_ChatManager, ciMultiInstance, tmApartment); end. |
type TChatRoom=class private FBuffer:array[1..20] of string; FBufferLength:integer; FRoomName:string; FRoomID:integer; FLocked:boolean;//同步锁,用来处理多人同时发出对话的情况 FConnectCount:integer;//当前房间的人数 FClearBufferTag:integer; //每清空一次buffer此值便跳变一次,此脉冲被客户端检测 protected procedure ClearBuffer;//清空缓冲区 function GetCanRead:boolean; public constructor Create(RoomName:string;RoomID:integer); procedure OneSpeak(content:string);//将一条聊天内容加入缓冲区 procedure LoginRoom(UserName:string);//参看实现部分注释 procedure LeaveRoom(UserName:string);//参看实现部分注释 function OneRead:Tstrings;//从缓冲区中读出记录 property Locked:boolean read FLocked; //readonly;//供IChatManager检测 property CanRead:boolean read GetCanRead;//判断缓冲区是否为空,否则是不可读的 property ClearBufferTag:integer read FClearBufferTag; end; TchatRoom的实现: { TChatRoom } constructor TChatRoom.Create(RoomName:string;RoomID:integer); begin FBufferLength:=0; FConnectCount:=0; FClearBufferTag:=1; FLocked:=false; FRoomName:=RoomName; FRoomID:=RoomID; end; procedure TChatRoom.ClearBuffer; var i:integer; begin ///在这里可以检测一个标志,判断是否需要服务器记录每一次聊天内容 for i:=1 to 20 do FBuffer[i]:=''; FBufferLength:=0; FClearBufferTag:=0-FClearBufferTag; end; procedure TChatRoom.OneSpeak(content:string); begin FLocked:=true; inc(FBufferLength); if FBufferLength>20 then begin ClearBuffer; inc(FBufferLength); end; FBuffer[FBufferLength]:=content; FLocked:=false; end; function TChatRoom.OneRead:TStrings; var FStrings:TStrings; i:integer; begin FLocked:=true; FStrings:=TStringList.Create; for i:=1 to FBufferLength do FStrings.Add(FBuffer[i]); result:=FStrings; FLocked:=false; end; function TChatRoom.GetCanRead: boolean; begin result:=false; if FBufferLength>0 then result:=true; end; procedure TChatRoom.LoginRoom(UserName:string); //用户登陆聊天室事件,这里没有完全实现 begin inc(FConnectCount); end; procedure TChatRoom.LeaveRoom(UserName: string); //用户离开聊天室事件,这里没有完全实现 begin Dec(FConnectCount); end; 服务器端的最后一个比较重要的部分TchatRoomManager: type TChatRoomManager=class private ChatRoom:array of TChatRoom; public constructor Create; function FindRoomByID(id:integer):TChatRoom; end; 实现部分: { TChatRoomManager } constructor TChatRoomManager.Create; var i,RoomCount:integer; RoomNames:TStrings;//RoomName是配置文件中的聊天室名称 begin RoomCount:=1; //这里将从配置文件中读出有几个聊天室 RoomNames:=TStringList.Create; RoomNames.Add('TestRoom');//这句将被最终的从配置文件读取替换掉 setlength(ChatRoom,RoomCount); for i:=1 to RoomCount do ChatRoom[i]:=TChatRoom.Create(RoomNames[i-1],i); end; function TChatRoomManager.FindRoomByID(id:integer): TChatRoom; //该函数由IChatManager接口调用,由于最终版本的接口将会提供给客户 //端得到房间列表的功能,所以客户端知道自己房间的id begin result:=ChatRoom[id]; end; initialization ChatRoomManager:=TChatRoomManager.Create; end. |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者