1 00:00:05,970 --> 00:00:09,510 This function will also be used by our server and client. 2 00:00:09,510 --> 00:00:13,040 So again we will add this function to our utils file. 3 00:00:13,050 --> 00:00:15,840 We will need another use statement. 4 00:00:15,840 --> 00:00:17,520 So we'll say use. 5 00:00:18,560 --> 00:00:19,610 C or D 6 00:00:22,760 --> 00:00:26,510 Day and D serialized. 7 00:00:28,310 --> 00:00:35,060 So the function we are going to implement is going to be relatively similar to our second JSON function. 8 00:00:36,040 --> 00:00:38,380 We are going to have to generic types. 9 00:00:38,380 --> 00:00:43,810 One generic type is going to implement the asynchronous buff, read and unpin. 10 00:00:44,260 --> 00:00:50,950 Our other generic type is going to need to make sure the serialized owned is implemented, which is 11 00:00:50,950 --> 00:00:54,160 a more strict version of the serialized trait. 12 00:00:54,250 --> 00:01:01,600 The reason we are doing this is so that our serialized values being returned to our caller arc. 13 00:01:01,630 --> 00:01:06,940 The client need to outlive the buffer we are getting our messages from. 14 00:01:07,030 --> 00:01:14,020 So serialized own is always going to be independent from the buffer, which means we will satisfy that 15 00:01:14,020 --> 00:01:14,890 requirement. 16 00:01:15,130 --> 00:01:17,350 So let's begin implementing this. 17 00:01:21,560 --> 00:01:25,430 So we'll have our receive function with two generics. 18 00:01:27,510 --> 00:01:29,280 And then we have incoming. 19 00:01:30,380 --> 00:01:31,010 I. 20 00:01:33,470 --> 00:01:36,920 And then we are going to implement a stream. 21 00:01:38,520 --> 00:01:43,050 Where the item is of a chat result. 22 00:01:46,310 --> 00:01:52,490 Where, and now we begin to add in our restraints. 23 00:01:52,490 --> 00:01:56,210 So I needs to make sure that. 24 00:01:59,770 --> 00:02:02,470 Read and unpin. 25 00:02:03,880 --> 00:02:11,290 Are satisfied and then t needs to make sure d serialized owned is satisfied. 26 00:02:13,890 --> 00:02:25,260 And now we will take our incoming lines and we're going to map these line into a closure. 27 00:02:29,120 --> 00:02:31,670 Chat result he. 28 00:02:35,520 --> 00:02:44,430 And then here we're going to say let lie equals line and then let message Saturday JSON. 29 00:02:46,370 --> 00:02:48,200 Saturday nights and 30 00:02:52,790 --> 00:02:56,690 Saturday JSON from string. 31 00:02:59,950 --> 00:03:08,170 Of our generic tea and a reference to our ally. 32 00:03:08,560 --> 00:03:11,920 And then we will return an oak message. 33 00:03:13,540 --> 00:03:15,190 So incoming lines. 34 00:03:16,790 --> 00:03:21,250 Right here is giving us a stream of string values. 35 00:03:21,260 --> 00:03:25,160 We then map these values and propagate the errors if needed. 36 00:03:25,160 --> 00:03:31,160 And assuming there are no errors, we will then get the string form back from the JSON value. 37 00:03:31,520 --> 00:03:40,100 From that we get a stream of chat result values, which is why our return type is a stream of chat results. 38 00:03:41,320 --> 00:03:47,110 If we examine our function closely, we can see that the function itself is not really asynchronous. 39 00:03:47,350 --> 00:03:53,920 The only thing asynchronous about it is the fact that we return a stream which is asynchronous. 40 00:03:55,160 --> 00:04:00,650 But now that we have our receiving function done to get messages from the server, we can use this in 41 00:04:00,650 --> 00:04:01,280 our client. 42 00:04:02,330 --> 00:04:07,910 So the function in our client to receive messages is going to be pretty straightforward. 43 00:04:08,240 --> 00:04:15,260 We will have one parameter that is going to take a socket to receive the message from and then create 44 00:04:15,260 --> 00:04:17,030 an asynchronous buffer reader. 45 00:04:18,440 --> 00:04:24,530 Then we will pass that data to our receive function in utils and order. 46 00:04:25,400 --> 00:04:27,200 You get our stream of messages. 47 00:04:28,690 --> 00:04:34,300 Once we start to receive those messages, we will just match on the types and begin printing out the 48 00:04:34,300 --> 00:04:35,320 appropriate data. 49 00:04:35,950 --> 00:04:40,900 If you would like to try to implement this on your own, take a second to pause the lecture. 50 00:04:41,170 --> 00:04:47,290 If not, I'm going to head over to our client file now and begin implementing this. 51 00:04:48,200 --> 00:05:01,640 So we have async messages where we're going to have the server of net TCP stream and this is going to 52 00:05:01,640 --> 00:05:05,270 return a chat result. 53 00:05:09,890 --> 00:05:11,930 So now we're going to have our buffer. 54 00:05:15,950 --> 00:05:16,880 Getting our. 55 00:05:18,540 --> 00:05:23,130 Tough reader, creating a new one on our server variable. 56 00:05:24,180 --> 00:05:31,650 And now we'll have our stream which we are getting from our utils receive function. 57 00:05:34,180 --> 00:05:40,420 So while some message equals. 58 00:05:41,840 --> 00:05:48,470 Stream dot next dot await. 59 00:05:49,970 --> 00:05:52,700 We want to match on the message. 60 00:05:54,080 --> 00:05:57,440 So server message. 61 00:05:58,640 --> 00:06:05,510 We have our chat name and our message. 62 00:06:08,400 --> 00:06:13,200 And it's going to print out print line. 63 00:06:16,130 --> 00:06:18,830 And then we're going to have chat name. 64 00:06:18,860 --> 00:06:22,700 So we're going to print out our chat name and then we'll do a new line. 65 00:06:22,700 --> 00:06:23,960 That way it's all separate. 66 00:06:24,080 --> 00:06:27,470 And now we're going to also print out the message we received. 67 00:06:32,640 --> 00:06:36,990 And so it will act and then we'll put us on a new line after. 68 00:06:37,740 --> 00:06:41,370 So we have that name and. 69 00:06:44,350 --> 00:06:45,250 Message. 70 00:06:47,860 --> 00:06:52,090 Otherwise if it is a server error. 71 00:06:55,100 --> 00:06:56,180 Message. 72 00:06:57,590 --> 00:07:11,000 We will return a print line saying air received and then we want to print out that message. 73 00:07:13,700 --> 00:07:16,370 And fix that typo. 74 00:07:19,690 --> 00:07:26,200 And if all once we're all good there, then we just return. 75 00:07:27,890 --> 00:07:34,100 So now we have our implementation for receiving messages from the server to our client. 76 00:07:34,100 --> 00:07:40,550 So we will take a pause here and then continue working on our client in the next lecture.