Before we continue, I want to address a couple of mistakes that I realized that I made that way.
Our program will work smoothly by the end of this lecture.
Inside of our chats file.
We need to add this use statement so that we can bring receive air into scope.
That way we are able to use it down here in our sub function.
We also need to change arc string to arc leven and our join method and then we need to remove from our
join method that we were returning a chat struct.
And that's all we needed to do inside of this file, inside of connection.
I realized that I left off that question mark on this line.
So if you did as well, make sure you add that in.
And then up here we need to bring our mutex into scope into our async standard library.
As well as chat tracker from our chats map crate.
And then once you've done that, we are all squared away to be able to start implementing in our logic
to tie everything in together and have a working application.
So the first thing that we need to do is bring some items into scope, such as task
chat result.
Sink, Ark.
And then use standard net net and.
Eight six standard.
There we go.
Net.
Now we want to bring reference our connection file, our chats file and our chats map file.
We want to bring in our connection.
Handle, function.
That way we can just say handle instead of typing out connection handle.
And now we can create main which is going to return a chat result for us.
And this is going to be set up very similar to what we did inside of our main function and client.
We're going to have an address that we're going to get from the command line.
Expect.
And then if we don't get what we expected, we want to print out that that is what we wanted.
So now we're going to have our chat table equals to a new atomic reference counter.
Chat, snap chat tracker and we want to create a new table and now we'll call async standard task block
on.
We're calling asynchronous code so we need an asynchronous block and now we're going to create our listener.
And bind it to the address we received from.
Our user.
And then we're going to create mutable new connections equals to listener incoming.
And now a while loop while we let some.
Socket result.
Equal to new connections.
Next
data.
Wait on some new connections.
We're going to say socket equals to socket result.
Let chat equals to chat table dot clone.
And now we want to spawn an asynchronous.
Task log air in this case on handle, which is the function we created in connection socket chats.
Dot.
Wait.
And then if all goes well, we want to return.
Okay.
It helps if I spell these right.
Listener.
Next.
Method found.
Met Incoming new connections.
Listener dot incoming.
So let's see what's going on here.
So to fix next.
Saying that it's not available in scope.
We need to add in use async prelude and then again everything.
And now we need to implement our log error function, which is going to be a super simple function for
us.
It's just going to be a result of a type chat result.
And we'll say if let air.
Of air equals result.
Now, we just want to print this out for us.
And now we should have a working server and client, so let's see it in action.
So we're going to run cargo run, tac tac release, tac tac fin server.
So this is telling cargo to build our server executable and run it and we want to run it on local host.
Port, 80, 80, and now this is going to start building everything.
So while it builds, I want to bring up a new terminal CD into the appropriate directory.
And this time I want to create a client.
And we're waiting for it to finish building here.
So we have one client.
And now I want to also.
Create.
Another client.
And now what we can do is say join.
And we'll call this chat one.
And we'll come back to our other client, say join chat one.
So now both of our clients are in the same chat room and so we'll save post to chat one our message.
So we'll say client two's message.
And if we go over to client one, we'll see that our message printed out.
So now if we say post
chat one and I will say client one's message, we should see it on client two.
And we do.
So congratulations on creating an asynchronous client server chat application.
I hope you enjoyed creating this and learned a lot in the process.
Please feel free to ask any questions about anything we did during this project.