Placeholder Image

字幕列表 影片播放

  • Welcome back, everybody.

  • In today's video, we're going to be going back the real time chat application and adding in chat rooms.

  • Support to it.

  • So let's get started now.

  • First, let's do a quick demo vote we're going to create.

  • As you can see, we have the ability to create a new room in our case, what is?

  • Call it new, and when we create that, you see it pops up over on the other screen immediately without having to refresh, which was typing a user name here and now we can join that room, type in another name, and you can see that we can send messages back and forth between each other in that room.

  • But if we, for example, when I created a new room and we typed in the name and tried to send a message, you'll see that doesn't get sent to the other room.

  • And all of our rooms are distinctly unique from each other, and they don't get messages from the other rooms to get started.

  • I have our project from the previous video, which, if you haven't already seen, make sure to check out the cards and the description for a link to that.

  • But essentially what we've done in this video is we can open up our project using live server and we can run a command and PM run Dev start and we're gonna start up our server as well as the start of our application, and we can send some messages back and forth.

  • We could say what our name is, for example, and send messages back and forth that way, but we need to do in order to set up a room.

  • Support is we actually need to convert our entire application to using express and everything will run to the console when we started our server and we have no need for a life server.

  • So to do that, what's close out of our consul here and we need to run a command and P m i in order to install our dependencies.

  • And we need to insult Express as well as E J s, which will be the temple waiting language that we use.

  • Express is going to allow us to make our server set up really easy, and e j s will allow us to embed service side code into our templates.

  • Now that's done being installed.

  • What's open up our server dot Js file and configure this to use express.

  • The first thing we need to do is we actually to require express, so we'll just come appear Credit variable called Express.

  • We could just type in, require express.

  • And what we want to do is we actually want to get our server set up from that.

  • So we first created very bold words we're gonna call APP and we're gonna set that equal to express just as a function.

  • And that's going to be the app for our server.

  • And then to get our server, which we need to pass into here, we need to create a variable called server to pass to our soccer IO connection.

  • We'll just create that variable server and we'll just require http which is the basic library from Know Js for doing http, we can call the server function on it for the class and passage in our application here from Express.

  • This will give us a server that can communicate with socket io and allow us to do all these io dot on connection things down here.

  • The next thing we need to do is actually set up our express server.

  • So we want to come in here and type in ap dot set and we want to set where our views were going to be coming from and our views were just going to come from a folder which is just going to be called views.

  • And we can create that folder now, so type interviews, and we want to move our index dot html into here.

  • But instead of it being an HTML file, we actually need this to be in e Js file.

  • So what's just changed the extension here, T.

  • J.

  • S because we're using E.

  • J s as our template language.

  • Now, let's go back to our server, and we actually need to tell it to use E.

  • J s.

  • So we'll say apt upset again.

  • We want to set our view engine, and this will tell it what to use.

  • Two parts are views, and in our case, that's going to be E.

  • J s, which we installed within PM.

  • Lastly, we need to set up a few more things, so we need to set up a public folder, which is where our job script is gonna go.

  • So we can say express dot static and we just type in the name of where Public Folder is going to be.

  • In our case, it's just going to be called public, so we could just type that in, and this is going to be where all of our JavaScript goes.

  • So let's create that folder now call public and we'll drag in our script.

  • Ah, jazz, which is our JavaScript for the client side.

  • And then lastly, we want to build to accept your rope parameters.

  • So what was going to do here again?

  • App that use, We want typing express euro encoded on.

  • We need to pass it in here and options object which is going to have extended set to true.

  • And this is going to allow our application to use euro encoded parameters instead of a body for a form.

  • Now, with the very basics of our server set up, let's create the routes that we're going to use first.

  • We're just going to have a simple route here, which is a get route on the index path is going to take a function here with a request as well as a response.

  • This is going to very simply, just render index page.

  • So we'll just say reds, which is our response that render we want to render our index page.

  • And in here we're going to pass it all the different rooms we have.

  • So we'll just say rooms is going to be equal to rooms.

  • This is going to pass down to our view, to be able to be used there.

  • So it's actually created variable, or they're gonna call it rooms and initialize it to an empty object.

  • Since at the beginning, we want our application to have zero rooms to find and then we need to create another out.

  • This is again we're going to be a get route and this is going to be forgetting a room.

  • So we wanna have slash here and then room.

  • And this is going to be a parameter that gets passed in the euro.

  • As you can see here we have the room name in our bureau, and that's what this is defined as.

  • Everything that is a room name will be passed here in any route that is a slash, followed by any name will go to this route and again, this is going to take a response and a request variable inside of our You are over out here and what we want to do in here is we just want to render another index page.

  • But this time, instead of index, it's going to be called Room.

  • So we're going to render a page called Room and in here we want to pass our room name, which is going to be inside of an object.

  • So we'll say room name.

  • And this is going to be equal to our request dot programs dot room.

  • And this corresponds to our room variable that we defined in our route here.

  • Now it's actually create that file.

  • We're just gonna call it room dot t j s and we're not gonna touch that for now.

  • So now if we save her application and actually run it using MPM Run, Deb, start.

  • Let's start over here.

  • Close that bet.

  • And we just want to make sure this is Port 3000 and we run.

  • That will see that we are getting an error saying that a page cannot be reached, and that is because we actually need to tell our server toe.

  • Listen on the correct port So we have our server variable here.

  • So all the way down here, Pushers say hopes server dot Listen, we wanna listen on Port 3000.

  • Now, we can save that.

  • Refresh our page over here, and you see, our application loads up justice before and if we type in a name, everything works is before, you know, before we get diving too far into our actual server code, let's work on creating the index page of our application because we want this to render our rooms and not our actual application here.

  • So we can actually change this file here.

  • To be our room file on our new room file is going to be our index file.

  • So let's do that real quick.

  • Will rename our Index Don t.

  • J s to be room dot you Jess and will create a new file, which is going to be index dot e.

  • J s.

  • And this is going to be all over index code.

  • We can copy over most of this document code, so it's copy that over, Take our head, which is going to be closing out her head and then we want to open up the body which is the actual content of her application.

  • Make sure remember to close that out and remember to close out our HTML tag and were mostly copying this over so that we get the same scripts of being brought over into our application Now, inside of the body is going to be really straightforward.

  • We first, they're gonna have the diff here, which we're gonna call an I D.

  • Of room container.

  • And this is where the list of all of our different rooms air gonna go.

  • And if you remember inside of our survey here, we passed in this room's variable to our application, which is an object.

  • And the key of this object is going to be the name of our rooms.

  • So, for example, we're going to have a name here which should be the name.

  • And then we're gonna have an object here for each one of them.

  • So we'll have, like, room name.

  • And if we had another room name, name to it would look like this.

  • So we just need to get the keys of our different objects are going to be the different rooms we want to display.

  • So what's actually leave a room in there so we can actually test what this will look like when we render our page.

  • Now back in our index here, we want to look through those.

  • So to get the keys of an object, all we needed to do is called object dot keys method and we're gonna pass it in that variable, which in our case, is rooms.

  • Then we looked through them using it for each.

  • We're going to get a single room for each one of these, and inside of that for each one of our rooms.

  • We just want to render a diff which is going to be having the text of our room in here so we can say a room, close that off and make sure to close off the actual div.

  • And then we need a link that's going to allow us to enter that room.

  • In our case, we're just gonna use an anchor tag has an eight ref here, which is going to be slash followed by our room.

  • So in our case again, we're just gonna have room being deployed here just like that.

  • And we want the text for this to say join so close off that anchor tag, and we got to make sure that we close off her actual function here, too.

  • So just like this, we're going to add in that bracket.

  • There we go.

  • That's actually closing off our loop.

  • Now that's all the code to rent our rooms.

  • But save that refresher application.

  • And as you can see right here, we're getting the name of the room as well as the joint button, which redirects us to the route with name in it.

  • We're getting an error here.

  • That's just because we haven't properly implemented this route yet, which is perfectly fine.

  • Now we can go back on.

  • What we want to do is we want to create a form here which is going to allow us to create new rooms.

  • So let's do that really quick.

  • We want to use a form tag.

  • We need the action, which is going to be where our form is going to round two.

  • And our case is just going to be a slash room and we also need a method because this is going to be a post method since we want to create something and post is the way to create things with http, Close off that form.

  • Inside of this form, it's gonna be really simple.

  • We're just gonna have a single input which is going to have a name of room and we're gonna be able to access this input variable on our form are on our server.

  • By using this name here of room, you'll see that it's a little bit and we also want to make sure the type of this is equal the text since it's just a text input and lastly, we're gonna make it required so that it's required by the actual HTML to be submitted before we can submit our form.

  • Now it's created button here where you're going to have a type of submit so we can submit our form and we're just gonna get the text of new room and lastly, close off that button.

  • And now when we save that and refresh over here, you can see that we get that section and we'll free type in here and hit new room.

  • It's going to bring us to our new room route, which we haven't created yet.

  • So inside of our server was due that we want to do an app dot post senses the post route, and it's gonna be posted a slash room.

  • As you can see here and again, this is going to take the same parameters request and response as a function.

  • And inside here, all we need to do is actually at our room to this room's variable.

  • In order to do that is really straightforward.

  • We just access our room is variable and we just say request dot body dot room and this dot room is the name of this variable.

  • As you can see here, we set the name to room.

  • So we access that with request stop body and in the name room, that was we just want to set that equal here to a new object.

  • And our case, our object is just going to have a user's variable, which we're going to default, to know users to being with so just an empty object of users And what the thing that we want to do as we want to redirect the user to that new room.

  • So we're just going to say response dot redirect and we want to redirect into that room name, which in our case, is response.

  • Stop body in that room again.

  • And lastly, we're going to eventually not right now, but we're eventually going to want to submit this down to our socket.

  • So we're gonna just say, send message that new room was created and this is going to be so that our page on the other person's browser, for example, this user here, it'll automatically add that room to their page whenever a new broom is created.

  • But we'll do that later when we work on handling our room integration.

  • And one other thing we want to do as you want to check to make sure this room doesn't already exist because otherwise will override a room if it has the same name.

  • So let's do a simple check here and just say rooms of request that body dot room.

  • We want to make sure that this is not equal to know, and if it's not equal to know, it means a room already exist and we want to exit out.

  • So to exit out, we're just going to use return, and we're just going to simply redirect the user back to that index page so that they know that that room has already been created.

  • So now it's actually test that we'll go back here refresher application, and we're gonna create a new room.

  • We're gonna call it noon, hit new, and you can see it brings us to that new room just here.

  • Have we go back here and refreshes?

  • You'll see that new room is showing up in the list.

  • But if we tried to create a room with the exact same name, for example new, we click new room, you see that it doesn't get created, and it redirects us back to this page so that we know that we need to create a room with a different name.

  • Now, with all that out of the way, you may have been noticing.

  • Every time we refresh this page, we get the prompt to enter our name, which we only want to get on the form first submitted messages, some inside of our JavaScript here inside of our script tag.

  • We actually want to make sure that this code here for rendering what our name is only gets run if we actually have a form to be using it on.

  • So we could just use a simple statement.

  • We want to check to see if, for example, this message form exist.

  • So we're gonna say if message form is not equal to know, then we actually want to run the code inside of here for actually prompting a user for the name.

  • Also, we only want to run this code for adding the event listener for our message form if we actually have that message form, so we're gonna move that up inside of her.

  • If statement as well, all the rest of the socket connections could be done on any page, and it really doesn't matter.

  • It's now it's saved that code.

  • Come over here and refresh our page.

  • You'll notice we're no longer getting that annoying message.

  • Now let's jump back to our server and actually set up our room because right now we go to you, see that we get in there and the reason for this air is really simple.

  • I just have our request and response variables foot here inside of our function.

  • So now if we say that and refresh over here you see, we no longer get that air.

  • And as you can see our pages working with our messages and we can send messages back and forth, but right now they're not constrained to a single room, which is fine for now.

  • Let's finish out this function so that we redirect the user back to the home page if, for example, of room does not exist with the name that supplied.

  • So in order to do that, it's really simple only you to do this.

  • Check to see if that room exists.

  • So we go to our rooms variable.

  • We want to check if the request stop.

  • Paramus dot Room is equal to know essentially, what this check says is if a room does not exist.

  • What we want to do is we want to redirect our user.

  • So we're getting used return again.

  • We could just say reds dot redirect and we're to redirect them back to the home page.

  • And we're only gonna do this if the room doesn't exist.

  • So, for example, we could just type in a random name up here for a room that we know doesn't exist.

  • And when we hit enter, it'll redirect us back to the home page because we tried to access a room that doesn't exist.

  • But if we access the room that does exist.

  • As you can see, it allows us through just fine.

  • Now we can finally jump into the fun part of actually doing real time integration, since we have the server code done and the easiest first thing that we can do is sending the message to the user when a new room is created.

  • So to do this we need to do is use our io object, which comes from socket Io.

  • And this allows us to send out a message to all of our users by just doing io dot admit.

  • And in here we can send a name which is just going to be room created.

  • This works just like all of our other sock it emits down here below.

  • And we want to send the perimeter, which is going to be the name of the room which in our case, is just going to be request stop body dot room Now in our actual JavaScript, we want to make sure we handle this room created message.

  • So let's go into the script file here.

  • And we could just set up a very simple socket dot on and we want to do it on from created and we know this is going to take our room name inside of here.

  • Now with this room name, what we want to do is we want to create a few elements.

  • The first element we're going to create is just our room element, which is just going to be the name of the room.

  • And we want this to just be document dot create element, and we want it to be a div.

  • This is going to match our format down here instead of her index dot You, Jess, it's going to match this exact foreman down here, so I'm actually gonna copy this over so that we can see exactly what we're trying to format.

  • Here we go on comment that out so we can see exactly what we're trying to work on.

  • Now, we could take that room element we want to do is we want to set the inner text equal to that room that we just got.

  • As you can see here, we want the name inside that div to be our room.

  • Next, we need to create our link.

  • So we're just gonna create a variable called room link.

  • We're gonna set that equal to document dot Korea Element hopes three element.

  • We want this one to be an anchor tag.

  • And then with this room link, What we want to do is we want to first set th rough here to be equal to slash our room.

  • So we can just say dot a trip is going to be equal to We're gonna use string interpolation a slash followed by our room name, which in our case, is just going to be that room variable.

  • Next, we want to come into our room link again and we want to set the inner text.

  • And this inter text is just going to be joined because, as you can see, that's where we're using here and now we have all of our code for actual element linked up we need to do is we need to add these to our page.

  • So if you remember in her index, we have this room container variable that we used for Outer Div.

  • So we can actually access that by creating here a room container variable and this room container variable is going to be getting element by the I d of room container.

  • Now we can come down here and use that room container and we could just upend elements to the bottom of it.

  • First, we're gonna attend our room element and the next we're going to upend our room link.

  • Now, if we save that, go back to index page here.

  • And let's say that we wanna refresh both of these.

  • So we have our fresh JavaScript and we're gonna create a new room called New Clique New Room and you could see that pops up down here.

  • But one problem is our actual form is being shown inside of it.

  • So let's go back under index page here and take our form.

  • We want to move it outside of our room container just like that.

  • Make sure everything's in dead did properly.

  • And let's do a refresh of all of our pages, go back to the index and will create another room.

  • We just call this one another.

  • And now, as you can see, that's properly being added to the bottom of the list, but before and put here for creating a new room.

  • So now integrating our new rooms is done, and all we need to do is working on making sure I messages only show up in the proper rooms.

  • In order to do that, we first need to pass up the room name for the room that we're currently on with every single request that we're using, for example, for chat messages, user connected and so on.

  • So when we're admitting to our server, we need to not only pass the message along, but we also need to pass along the room.

  • So what's going here?

  • We're gonna pass among the room name with every single one of these.

  • We're gonna do the same thing for a new user just like this.

  • But this room named Variable, is not to find anywhere.

  • So we need to actually to find that.

  • And if you remember in our server code were passing the room named down indoor e Js file.

  • So inside of our room di e gs at the very top here, above all of our different scripts, we're just going to create a simple script tag and inside of this script, take we're just going to create a variable, which we're just gonna call room name.

  • We're gonna use our e j s in order to set this room name equal to the room Name that our servers passing down so we can use our fancy E.

  • J s symbol syntax here in order to actually access that by using these percent symbols and such, this is going to access our server variable and set it to a JavaScript variable, which we can use on the client side.

  • Now, if we close out that script, we now have our room named Variable, which is going to be accessible from anywhere in our application after this script tag.

  • Now it's saved that go back into our script here.

  • And this room named Variable is going to be the same room name as it's passed down from our server.

  • And that's all the work that we actually need to do on our client's side, because those are the only two messages we sent to our server now inside of the server.

  • We need to handle that room name properly.

  • So as we could see, we have our new user here, and this is going to now take not only a name but also the room that that name is being sent to.

  • Same thing with our send message This again is going to take a room as well as the message is going to be sent.

  • And now to send that specific message to the room instead of broadcasting to everyone, we want to make sure we only broadcast to specific people.

  • So we say, socket 0.2 and we pass in the name of the room we want to broadcast to.

  • And this is only gonna send messages to the users inside of that room and no one outside of that room.

  • Let's do the same thing up here we can say to bring, and this again is going to send that message to everyone inside of that room and no one else.

  • One thing to note, though, is that we're not actually adding our users to any of these rooms ever.

  • We need to actually call socket dot join to tell our users to join a specific room.

  • So when we get this new user request sent to our server we want to do is we want to take that users socket and we're gonna call join, and we're gonna pass in the room.

  • We want them to join, and essentially, what this is going to do is it's going to say, Send this user into this room so that when we call dot to room, it's going to know that that user is in that room so that any time we send a message to that room, that user is going to get it.

  • And when the user disconnects down here, it's automatically going to remove them from the room so we don't actually have to handle that ourselves.

  • Next.

  • This users variable we're no longer going to use because we only want to use the user's variable inside of our rooms.

  • Variable up here.

  • We can remove this name one for now.

  • And as you remember, we defaulted.

  • An empty users object for each one of our rooms, and we only want to store the user's for each specific room that they're inside of.

  • So when we get a new user here, we have she needed first access our rooms variable for a specific room and then get the user's for that room and add that user to that room's users instead of just a global list of users Again.

  • Any time we use this, users variable down here.

  • We need to first access the room that we're getting the user from and then get the user's from that room to find the specific user.

  • And again, we're gonna do the same thing down here and down here.

  • One thing to note, though, isn't our disconnect.

  • We don't actually have a room variable being passed.

  • We need to get all the rooms that the user's air end currently.

  • So we're going to create a function or judgment call, get user rooms.

  • We're gonna pass it in the socket that we want to use, which is essentially going to indicate our user hopes to make sure that this is a capital you here.

  • And what we want to do is we want to loop over.

  • All these rooms were going to say for each room inside of that list, and then we're gonna have access to that room variable, and we can execute the code down here, which is going to broadcast the user disconnect as well as delete that user from that users rooms.

  • So it's create that function.

  • Now there's going to be a little bit of a complex function, but I'll walk you through it step by step, so we're gonna create user forms.

  • Here we go and then it's going to take a socket.

  • And inside of this function, what we want to return is we want to return all of the users rooms that they're in.

  • So we can say object dot entries and we want to pass it in all of the rooms.

  • So what this is going to do is going to convert our object to in array, which we can use inside of the method, which we're gonna use the reduce method for and reduces a slightly complex method.

  • So if you don't fully understand it, make sure to check out my top eight of Ray methods video, which fully explains how to use reduce.

  • But essentially what we're going to do is reduce is going to take as its first parameter a list of things that it's grouping by which in our case, is gonna be all the room names.

  • And the second parameter is going to take this object here, which is an array of a raise so we can use object destruction toe actually access the parameters of this ray.

  • The 1st 1 is going to be the key, which is our room name.

  • And the 2nd 1 is going to be the value which is going to be the room here.

  • Now it's actually set this up as an air of function, and inside of here is where all of our different codes gonna go.

  • But first, let's actually default.

  • This name's variable here to an empty array, bypassing that as the second parameter for reduced method.

  • And in here all we want to do is we want to check to see if room dot users because this is again the object for our room.

  • We want to make sure to see if we have a user with that socket I d.

  • So if we do have a user that is not equal to know here we want to take our names variable, which is going to be a list of all of the room names we want to just push in the name of the room, which we have right here.

  • And then we just want to return names down here so we can use this in the next iteration of our reduce method.

  • And essentially, what this method right here is going to do is it's going to check all of our rooms and all of our users and return the name of all of the rooms that that user is a part of which, in our case, should only be one single room.

  • Now, up here we have those names, and it's going to remove that user from all those different rooms, as well as send down the message saying that that users disconnected.

  • So let's save that and actually test these features in progress.

  • Let's go over to the index page for all of our different applications.

  • Here we go.

  • And let's throw out a couple other ones so that we can actually see what's going on.

  • Make sure we refresh all these and we're gonna create a new room called New and we're gonna first put a user in here.

  • And then we're going to join room new over here with this user and then create another room.

  • This was just gonna be called another, and they're going to be connected here.

  • And we also want to join another over here with this user.

  • And now, when we send messages across both these top browsers air in the same room.

  • So if we send a message Hi, you can see only the top browsers are getting that message.

  • And the bottom ones, they're not because they're actually in different rooms.

  • You can see when they send a message, it goes between each other.

  • And now when one of these users actually leaves the room, you see that is being propagated toe all of our browsers right now instead of just this single browser.

  • So let's look at why that is.

  • And as you can see, it's because we're broadcasting to all of our users and not to a specific room.

  • So let's make sure we add dot to room in here and say that and now everything will be working as it should.

  • Now, if we refresh all of our browsers, make sure we reset.

  • This situation is we had it before.

  • We're gonna set these bottom ones to new so they can join here, and we're going to set these ones to a different room.

  • Actually, give them a name that we go.

  • And now 11 of our users leaves, for example.

  • This top user leaves.

  • You should see that on Lee.

  • This top user gets the message, and none of these other bottom users are getting the message, and that's all it takes to set up basic room support in a real time chat application.

  • If you enjoyed that video, make sure check on my other project related videos linked over here and subscribe to the channel for more content just like this.

  • Thank you very much for watching and have a good day.

Welcome back, everybody.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

用Node.js和Socket.io構建實時聊天室。 (Build Real Time Chat Rooms With Node.js And Socket.io)

  • 0 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字