我正在学习如何使用HL7和IdTCPClient和IdTCPServer。收到HL7消息,并且我仅从第一条消息得到服务器的确认答复。但是此后,将收到消息,但不发送确认答复。它挂在AContext.Connection.IOHandler.WriteLn。如何使IdTCPServer对收到的每条消息发送确认答复?非常感谢您的投入。这是onExcute上的服务器端代码:
procedure THL7DM.IdTCPServer1Execute(AContext: TIdContext);
Function AcknowledgementMessage(HL7_msg: string): string;
var
s: TStrings;
MSA: TMSASegment;
MSH: TMSHSegment;
begin
result := '';
MSH := TMSHSegment.Create(HL7_msg); {HL7 MSH Segment}
MSA := TMSASegment.Create(''); {HL7 MSA Segment}
s := TStringList.Create;
try
MSH.Accept_Acknowledgment_Type_15 := 'AA';
MSA.Acknowledgment_Code_18 := 'AA';
MSH.Sending_Facility_4 := 'AEdge Lab';
MSH.Message_Type_9 := 'ACK';
MSA.Message_Waiting_Number_1827 := DateTimeToStr(now);
s.Text := MSH.ToString + #13 + #10 + 'MSA' + '|' + MSA.ToString;
s.Text := #11 + s.Text + #28 + #13;
result := s.Text;
finally
MSA.Free;
MSH.Free;
s.Free;
end;
end;
var
MsgStrings: TStrings;
s: string;
msg: string;
begin
MsgStrings := TStringList.Create;
s := AContext.Connection.IOHandler.ReadLn(IndyTextEncoding_OSDefault());
try
MsgStrings.Text := StrEscapedToString(s);
Form2.Memo3.Text := TRegEx.Replace(MsgStrings.Text, #11 + '|' + #28, '');
msg := AcknowledgementMessage(Form2.Memo3.Text);
if TRegEx.IsMatch(msg, #28#13) = True then
==> AContext.Connection.IOHandler.WriteLn(StrStringToEscaped(msg),
IndyTextEncoding_OSDefault());
if TRegEx.IsMatch(MsgStrings.Text, #11) = True then
SaveMessageToDatabase(MsgStrings);
finally
MsgStrings.Free;
end;
end;
这是客户端发送消息的地方:
procedure TForm2.BitBtn1Click(Sender: TObject);
var
LLine: String;
I: Integer;
s: string;
begin
// wrapping for HL7
LLine := #11 + Memo1.Text + #28 + #13;
if Receiving_System_Accepts_Escaped_Strings then
HL7DM.IdTCPClient1.IOHandler.WriteLn(StrStringToEscaped(LLine),
IndyTextEncoding_OSDefault())
else
HL7DM.IdTCPClient1.IOHandler.WriteLn(LLine, IndyTextEncoding_OSDefault());
if Assigned(ACKReplyHandler) = False then
begin
ACKReplyHandler := TACK_MsgHandlingThread.Create;
//This will handle incoming HL7 ACK replies
end;
end;
TACK_MsgHandlingThread看起来像这样:
procedure TACK_MsgHandlingThread.Execute;
begin
HandleACK_Replies;
end;
procedure TACK_MsgHandlingThread.HandleACK_Replies;
var
s: string;
begin
s := (HL7DM.IdTCPClient1.IOHandler.ReadLn(IndyTextEncoding_UTF8));
// ShowMessage(s);
s := StrEscapedToString(s);
s := TRegEx.Replace(s, #11, '');
s := TRegEx.Replace(s, #28#13#10, '');
Form2.Memo4.Clear;
Form2.Memo4.Text := (s);
end;
答案 0 :(得分:1)
TIdIOHandler.WriteLn()
可以阻止的唯一方法是,如果接收方没有读取已发送的数据,则导致其入站缓冲区填满并阻止发送方发送更多数据。这是因为您的TACK_MsgHandlingThread.Execute()
方法仅读取1条传入的答复,然后在Execute()
退出时终止线程,因此它将停止读取后续的答复。您需要在线程的生命周期中循环运行HandleACK_Replies()
的逻辑,对发送的每个答复调用TIdIOHandler.ReadLn()
,直到关闭套接字和/或终止线程为止。
此外,IndyTextEncoding_OSDefault
不能跨机器边界移植。但更重要的是,您正在客户端使用IndyTextEncoding_UTF8
。您需要在两侧使用相同的编码,否则可能会丢失数据。
此外,您的服务器正在访问Memo3
,而客户端正在访问Memo4
,而根本不与它们各自的UI线程同步。那是非常危险的。 VCL和FMX框架不是线程安全的(大多数UI框架不是),因此从UI线程外部访问UI控件时必须进行同步。