This function will also be used by our server and client.
So again we will add this function to our utils file.
We will need another use statement.
So we'll say use.
C or D
Day and D serialized.
So the function we are going to implement is going to be relatively similar to our second JSON function.
We are going to have to generic types.
One generic type is going to implement the asynchronous buff, read and unpin.
Our other generic type is going to need to make sure the serialized owned is implemented, which is
a more strict version of the serialized trait.
The reason we are doing this is so that our serialized values being returned to our caller arc.
The client need to outlive the buffer we are getting our messages from.
So serialized own is always going to be independent from the buffer, which means we will satisfy that
requirement.
So let's begin implementing this.
So we'll have our receive function with two generics.
And then we have incoming.
I.
And then we are going to implement a stream.
Where the item is of a chat result.
Where, and now we begin to add in our restraints.
So I needs to make sure that.
Read and unpin.
Are satisfied and then t needs to make sure d serialized owned is satisfied.
And now we will take our incoming lines and we're going to map these line into a closure.
Chat result he.
And then here we're going to say let lie equals line and then let message Saturday JSON.
Saturday nights and
Saturday JSON from string.
Of our generic tea and a reference to our ally.
And then we will return an oak message.
So incoming lines.
Right here is giving us a stream of string values.
We then map these values and propagate the errors if needed.
And assuming there are no errors, we will then get the string form back from the JSON value.
From that we get a stream of chat result values, which is why our return type is a stream of chat results.
If we examine our function closely, we can see that the function itself is not really asynchronous.
The only thing asynchronous about it is the fact that we return a stream which is asynchronous.
But now that we have our receiving function done to get messages from the server, we can use this in
our client.
So the function in our client to receive messages is going to be pretty straightforward.
We will have one parameter that is going to take a socket to receive the message from and then create
an asynchronous buffer reader.
Then we will pass that data to our receive function in utils and order.
You get our stream of messages.
Once we start to receive those messages, we will just match on the types and begin printing out the
appropriate data.
If you would like to try to implement this on your own, take a second to pause the lecture.
If not, I'm going to head over to our client file now and begin implementing this.
So we have async messages where we're going to have the server of net TCP stream and this is going to
return a chat result.
So now we're going to have our buffer.
Getting our.
Tough reader, creating a new one on our server variable.
And now we'll have our stream which we are getting from our utils receive function.
So while some message equals.
Stream dot next dot await.
We want to match on the message.
So server message.
We have our chat name and our message.
And it's going to print out print line.
And then we're going to have chat name.
So we're going to print out our chat name and then we'll do a new line.
That way it's all separate.
And now we're going to also print out the message we received.
And so it will act and then we'll put us on a new line after.
So we have that name and.
Message.
Otherwise if it is a server error.
Message.
We will return a print line saying air received and then we want to print out that message.
And fix that typo.
And if all once we're all good there, then we just return.
So now we have our implementation for receiving messages from the server to our client.
So we will take a pause here and then continue working on our client in the next lecture.