我使用Akka IO来连接Scala和C#应用程序。在scala端,我使用Akka IO将数据写入流。
case data: ByteString => connection ! Write(data)
但我想刷新缓冲区,因为在C#端我从缓冲区读取数据,有时我读取两条消息而不是一条消息,程序在解析时失败。 在C#方面,我以这样的方式阅读:
byte[] myReadBuffer = new byte[2048];
StringBuilder message = new StringBuilder();
int numberOfBytesRead = 0;
do
{
numberOfBytesRead = srReceiver.Read(myReadBuffer, 0, myReadBuffer.Length);
message.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, numberOfBytesRead));
} while (srReceiver.DataAvailable);
processMessage(message.ToString());
我想我应该在发送消息后刷新缓冲区,但我不知道该怎么做。 也许有人知道如何解决这个问题?
答案 0 :(得分:0)
private void ProcessMessage(String message)
{
string[] messages = message.Split(new String[] { END_PACKET }, StringSplitOptions.RemoveEmptyEntries);
foreach(String msg in messages)
{
string singleMessage = msg;
if (msg.Contains(END_PACKET))
singleMessage = msg.Substring(0, msg.LastIndexOf(END_PACKET));
notify("Received : " + singleMessage);
Request request = JsonConvert.DeserializeObject<Request>(singleMessage);
onMessageReseived(request);
}
}