1 00:00:05,960 --> 00:00:11,330 Before we continue, I want to address a couple of mistakes that I realized that I made that way. 2 00:00:11,330 --> 00:00:14,990 Our program will work smoothly by the end of this lecture. 3 00:00:16,300 --> 00:00:18,310 Inside of our chats file. 4 00:00:18,310 --> 00:00:24,430 We need to add this use statement so that we can bring receive air into scope. 5 00:00:24,850 --> 00:00:28,930 That way we are able to use it down here in our sub function. 6 00:00:29,860 --> 00:00:41,200 We also need to change arc string to arc leven and our join method and then we need to remove from our 7 00:00:41,200 --> 00:00:46,450 join method that we were returning a chat struct. 8 00:00:47,590 --> 00:00:55,150 And that's all we needed to do inside of this file, inside of connection. 9 00:00:55,990 --> 00:01:00,790 I realized that I left off that question mark on this line. 10 00:01:00,790 --> 00:01:03,400 So if you did as well, make sure you add that in. 11 00:01:03,940 --> 00:01:10,870 And then up here we need to bring our mutex into scope into our async standard library. 12 00:01:11,940 --> 00:01:15,810 As well as chat tracker from our chats map crate. 13 00:01:16,230 --> 00:01:22,110 And then once you've done that, we are all squared away to be able to start implementing in our logic 14 00:01:22,110 --> 00:01:26,550 to tie everything in together and have a working application. 15 00:01:27,120 --> 00:01:35,100 So the first thing that we need to do is bring some items into scope, such as task 16 00:01:38,160 --> 00:01:40,740 chat result. 17 00:01:43,230 --> 00:01:45,360 Sink, Ark. 18 00:01:47,380 --> 00:01:55,540 And then use standard net net and. 19 00:01:59,630 --> 00:02:01,400 Eight six standard. 20 00:02:03,880 --> 00:02:04,630 There we go. 21 00:02:05,380 --> 00:02:05,980 Net. 22 00:02:08,510 --> 00:02:18,560 Now we want to bring reference our connection file, our chats file and our chats map file. 23 00:02:20,030 --> 00:02:23,360 We want to bring in our connection. 24 00:02:23,360 --> 00:02:24,950 Handle, function. 25 00:02:26,130 --> 00:02:30,240 That way we can just say handle instead of typing out connection handle. 26 00:02:30,450 --> 00:02:37,890 And now we can create main which is going to return a chat result for us. 27 00:02:39,590 --> 00:02:45,470 And this is going to be set up very similar to what we did inside of our main function and client. 28 00:02:45,950 --> 00:02:50,570 We're going to have an address that we're going to get from the command line. 29 00:02:57,140 --> 00:02:57,980 Expect. 30 00:02:57,980 --> 00:03:04,820 And then if we don't get what we expected, we want to print out that that is what we wanted. 31 00:03:05,180 --> 00:03:12,380 So now we're going to have our chat table equals to a new atomic reference counter. 32 00:03:14,780 --> 00:03:28,370 Chat, snap chat tracker and we want to create a new table and now we'll call async standard task block 33 00:03:28,370 --> 00:03:28,970 on. 34 00:03:29,450 --> 00:03:39,080 We're calling asynchronous code so we need an asynchronous block and now we're going to create our listener. 35 00:03:43,090 --> 00:03:47,140 And bind it to the address we received from. 36 00:03:48,870 --> 00:03:49,560 Our user. 37 00:03:51,970 --> 00:03:59,950 And then we're going to create mutable new connections equals to listener incoming. 38 00:04:01,540 --> 00:04:05,470 And now a while loop while we let some. 39 00:04:07,720 --> 00:04:09,760 Socket result. 40 00:04:10,870 --> 00:04:14,380 Equal to new connections. 41 00:04:14,860 --> 00:04:16,000 Next 42 00:04:18,370 --> 00:04:18,730 data. 43 00:04:18,730 --> 00:04:20,320 Wait on some new connections. 44 00:04:20,320 --> 00:04:24,340 We're going to say socket equals to socket result. 45 00:04:25,940 --> 00:04:31,810 Let chat equals to chat table dot clone. 46 00:04:32,780 --> 00:04:38,450 And now we want to spawn an asynchronous. 47 00:04:39,240 --> 00:04:56,610 Task log air in this case on handle, which is the function we created in connection socket chats. 48 00:04:58,930 --> 00:04:59,170 Dot. 49 00:04:59,290 --> 00:04:59,830 Wait. 50 00:05:01,440 --> 00:05:08,030 And then if all goes well, we want to return. 51 00:05:08,040 --> 00:05:08,820 Okay. 52 00:05:09,570 --> 00:05:12,720 It helps if I spell these right. 53 00:05:12,750 --> 00:05:15,180 Listener. 54 00:05:16,860 --> 00:05:17,850 Next. 55 00:05:22,020 --> 00:05:23,820 Method found. 56 00:05:25,320 --> 00:05:28,110 Met Incoming new connections. 57 00:05:28,110 --> 00:05:30,810 Listener dot incoming. 58 00:05:31,620 --> 00:05:34,260 So let's see what's going on here. 59 00:05:45,920 --> 00:05:48,170 So to fix next. 60 00:05:49,220 --> 00:05:51,890 Saying that it's not available in scope. 61 00:05:51,890 --> 00:05:58,490 We need to add in use async prelude and then again everything. 62 00:05:59,640 --> 00:06:07,830 And now we need to implement our log error function, which is going to be a super simple function for 63 00:06:07,830 --> 00:06:08,370 us. 64 00:06:08,400 --> 00:06:14,010 It's just going to be a result of a type chat result. 65 00:06:17,070 --> 00:06:19,170 And we'll say if let air. 66 00:06:20,590 --> 00:06:26,380 Of air equals result. 67 00:06:26,410 --> 00:06:31,570 Now, we just want to print this out for us. 68 00:06:37,830 --> 00:06:44,700 And now we should have a working server and client, so let's see it in action. 69 00:06:45,060 --> 00:06:54,810 So we're going to run cargo run, tac tac release, tac tac fin server. 70 00:06:54,810 --> 00:07:03,330 So this is telling cargo to build our server executable and run it and we want to run it on local host. 71 00:07:06,700 --> 00:07:12,220 Port, 80, 80, and now this is going to start building everything. 72 00:07:12,220 --> 00:07:20,950 So while it builds, I want to bring up a new terminal CD into the appropriate directory. 73 00:07:21,130 --> 00:07:25,690 And this time I want to create a client. 74 00:07:27,530 --> 00:07:29,930 And we're waiting for it to finish building here. 75 00:07:29,930 --> 00:07:31,650 So we have one client. 76 00:07:31,670 --> 00:07:34,970 And now I want to also. 77 00:07:35,970 --> 00:07:36,660 Create. 78 00:07:37,380 --> 00:07:38,370 Another client. 79 00:07:39,730 --> 00:07:43,090 And now what we can do is say join. 80 00:07:44,080 --> 00:07:47,710 And we'll call this chat one. 81 00:07:49,660 --> 00:07:54,490 And we'll come back to our other client, say join chat one. 82 00:07:54,490 --> 00:08:02,950 So now both of our clients are in the same chat room and so we'll save post to chat one our message. 83 00:08:03,850 --> 00:08:10,090 So we'll say client two's message. 84 00:08:10,950 --> 00:08:16,950 And if we go over to client one, we'll see that our message printed out. 85 00:08:17,130 --> 00:08:19,350 So now if we say post 86 00:08:21,810 --> 00:08:32,070 chat one and I will say client one's message, we should see it on client two. 87 00:08:32,070 --> 00:08:33,660 And we do. 88 00:08:33,690 --> 00:08:41,250 So congratulations on creating an asynchronous client server chat application. 89 00:08:41,460 --> 00:08:45,450 I hope you enjoyed creating this and learned a lot in the process. 90 00:08:45,540 --> 00:08:52,290 Please feel free to ask any questions about anything we did during this project.