结合 Indy 和 ics 之间的编码
combine coding between indy and ics
我已经使用 Indy 一段时间了,现在我正在尝试使用另一个互联网组件,如 ICS 并结合 Indy 代码以在 ICS 我开始创建 TWSocketserver
并开始与客户端通信 Twsocket
我坚持一件事,在 Indy TIDTCPSERVER
我通常使用 AContext
来定义每个客户端连接服务器连接事件作为示例
TConnection = class(TObject)
Thread: Pointer;
end;
var
Connection : TConnection;
begin
Connection := TConnection.Create;
Connection.Thread := AContext;
AContext.Data := Connection;
end;
但在TwSocketserver
中是不同的参数。
我的问题是可以将上面的代码与 ICS 结合起来以获得相同的方法吗?
我尝试过的东西
procedure Tmainserver.serverClientConnect(Sender: TObject;
Client: TWSocketClient; Error: Word);
var
Connection : TConnection;
begin
Connection := TConnection.Create;
Connection.Thread := Client;
end;
而是客户端:TWSocketClient;有对象 class 被识别吗?
您可以从 TWSocketClient
派生一个自定义 class 并在调用 TWSocketServer.Listen()
之前将其分配给 TWSocketServer.ClientClass
属性,然后您可以添加您想要的任何内容想要您的 class,并在需要时通过对 TWSocketClient
对象进行类型转换来访问您的自定义 fields/methods,例如在 TWSocketServer.OnClientCreate
和 TWSocketServer.OnClientConnect
事件中。以下 EDN 文章中有一个示例:
Writing Client/Server applications using ICS
在你的情况下,尝试这样的事情:
type
TConnection = class
Client: Pointer;
end;
TMyClient = class(TWSocketClient)
public
Connection: TConnection;
end;
procedure Tmainserver.FormCreate(Sender: TObject);
begin
server.ClientClass := TMyClient;
end;
procedure Tmainserver.serverClientConnect(Sender: TObject;
Client: TWSocketClient; Error: Word);
var
Connection: TConnection;
begin
Connection := TConnection.Create;
Connection.Client := Client;
TMyClient(Client).Connection := Connection;
end;
procedure Tmainserver.serverClientDisconnect(Sender : TObject;
Client : TWSocketClient; Error : Word);
begin
FreeAndNil(TMyClient(Client).Connection);
end;
仅供参考,Indy 具有类似的功能。您在激活服务器之前将自定义 TIdServerContext
派生的 class 分配给 TIdTCPServer.ContextClass
属性,然后在需要时对 TIdContext
对象进行类型转换,例如在 TIdTCPServer.OnConnect
和 TIdTCPServer.OnExecute
事件:
type
TConnection = class
Context: Pointer;
end;
TMyContext = class(TIdServerContext)
public
// TIdContext already has a property named 'Connection'...
MyConnection: TConnection;
end;
procedure Tmainserver.FormCreate(Sender: TObject);
begin
server.ContextClass := TMyContext;
end;
procedure Tmainserver.serverConnect(AContext: TIdContext);
var
Connection: TConnection;
begin
Connection := TConnection.Create;
Connection.Context := AContext;
TMyContext(AContext).MyConnection := Connection;
end;
procedure Tmainserver.serverDisconnect(AContext: TIdContext);
begin
FreeAndNil(TMyContext(AContext).MyConnection);
end;
我更喜欢这种方法而不是使用 TIdContext.Data
属性.
我已经使用 Indy 一段时间了,现在我正在尝试使用另一个互联网组件,如 ICS 并结合 Indy 代码以在 ICS 我开始创建 TWSocketserver
并开始与客户端通信 Twsocket
我坚持一件事,在 Indy TIDTCPSERVER
我通常使用 AContext
来定义每个客户端连接服务器连接事件作为示例
TConnection = class(TObject)
Thread: Pointer;
end;
var
Connection : TConnection;
begin
Connection := TConnection.Create;
Connection.Thread := AContext;
AContext.Data := Connection;
end;
但在TwSocketserver
中是不同的参数。
我的问题是可以将上面的代码与 ICS 结合起来以获得相同的方法吗?
我尝试过的东西
procedure Tmainserver.serverClientConnect(Sender: TObject;
Client: TWSocketClient; Error: Word);
var
Connection : TConnection;
begin
Connection := TConnection.Create;
Connection.Thread := Client;
end;
而是客户端:TWSocketClient;有对象 class 被识别吗?
您可以从 TWSocketClient
派生一个自定义 class 并在调用 TWSocketServer.Listen()
之前将其分配给 TWSocketServer.ClientClass
属性,然后您可以添加您想要的任何内容想要您的 class,并在需要时通过对 TWSocketClient
对象进行类型转换来访问您的自定义 fields/methods,例如在 TWSocketServer.OnClientCreate
和 TWSocketServer.OnClientConnect
事件中。以下 EDN 文章中有一个示例:
Writing Client/Server applications using ICS
在你的情况下,尝试这样的事情:
type
TConnection = class
Client: Pointer;
end;
TMyClient = class(TWSocketClient)
public
Connection: TConnection;
end;
procedure Tmainserver.FormCreate(Sender: TObject);
begin
server.ClientClass := TMyClient;
end;
procedure Tmainserver.serverClientConnect(Sender: TObject;
Client: TWSocketClient; Error: Word);
var
Connection: TConnection;
begin
Connection := TConnection.Create;
Connection.Client := Client;
TMyClient(Client).Connection := Connection;
end;
procedure Tmainserver.serverClientDisconnect(Sender : TObject;
Client : TWSocketClient; Error : Word);
begin
FreeAndNil(TMyClient(Client).Connection);
end;
仅供参考,Indy 具有类似的功能。您在激活服务器之前将自定义 TIdServerContext
派生的 class 分配给 TIdTCPServer.ContextClass
属性,然后在需要时对 TIdContext
对象进行类型转换,例如在 TIdTCPServer.OnConnect
和 TIdTCPServer.OnExecute
事件:
type
TConnection = class
Context: Pointer;
end;
TMyContext = class(TIdServerContext)
public
// TIdContext already has a property named 'Connection'...
MyConnection: TConnection;
end;
procedure Tmainserver.FormCreate(Sender: TObject);
begin
server.ContextClass := TMyContext;
end;
procedure Tmainserver.serverConnect(AContext: TIdContext);
var
Connection: TConnection;
begin
Connection := TConnection.Create;
Connection.Context := AContext;
TMyContext(AContext).MyConnection := Connection;
end;
procedure Tmainserver.serverDisconnect(AContext: TIdContext);
begin
FreeAndNil(TMyContext(AContext).MyConnection);
end;
我更喜欢这种方法而不是使用 TIdContext.Data
属性.