Home >Web Front-end >JS Tutorial >Use socket to create private and public chat rooms in Node.js_node.js
I will show you the renderings first:
In the previous article, I introduced you to using Angular, Nodejs, and socket.io to build chat rooms and multi-person chat rooms. This article continues to introduce the use of sockets in Node.js to create private and public chat rooms. , please see below for specific details.
Among the applications of nodejs, socket should be more outstanding. socket.io has tens of thousands of stars on github. Its success should not be inferior to express. In order to facilitate the use of the entire socket.io .
For examples, please click http://chat.lovewebgames.com/
Source code downloadhttps://github.com/tianxiangbing/chat
Because I am too poor, the server and database are all free from abroad, and the access speed may be slightly slower.
Let me first talk about my understanding of socket.io. Websocket is more like opening a port service to monitor past communications. So we can rely on the current site port 80 to start the socket service, or we can put it on other ports, such as:
This is to monitor port 3000. Since the free server I use does not have permission to open other ports, I still use 80. Since 80 is already used by express, I have to pass it in when express is used. .
Then I wrote this in msg.js
This is harmonious. db is the method to create a mysql connection, which is not included in this section and is omitted.
This is how it works in socket.io. First, create a connection to the io channel, and then monitor the events of the socket inside. Nodejs is event-driven. The code is as follows:
As long as a user is connected at this time, it will enter the connection, and its parameter is a socket. If it is a public chat, we can use it directly
This is the form. But we are having a private chat here, so we need to temporarily save this socket object in the global world, so that the person you are chatting with privately can use it to find your socket. This is very convoluted. In fact, the private chat here is not completely point-to-point. It still passes through the server, and the message is transmitted to the server. The server then finds the socket object of the person you want to convey it to and sends it to him. This is the whole process. Here I use an array-like object to store it.
Since I need a username to log in, I use the username as the only identifier (this is just an example, don’t talk to me about duplicate usernames). The advantage of using an array-like form here is that I don’t need to loop. You can also find it quickly. When I send a private message to A, I will first find it in this uscoket, and then call its emit.
function sendUserMsg(data) { if (data.to in usocket) { console.log('================') console.log('to' + data.to, data); usocket[data.to].emit('to' + data.to, data); usocket[data.user].emit('to' + data.user, data); console.log('================') } }
The reason why I emit twice here is that when I send the message to the other party, I also need to receive the message myself and then display it. Why is this? First, the interface is unified, and all the content in the chat comes from the server. Second, it proves that I sent it successfully.
Then when I was listening on the client, I also used my own username to set up an event listener for the username.
socket.on('to' + user, function(data) { //console.log(data); formatMsg(data); })
In this way, whether it is a message I send or a message I receive, it will enter this event. Finally, don't forget to delete the object when the user leaves.
socket.on('disconnect', function() { console.log('disconnect') if (username) { counter--; delete users[username]; delete usocket[username]; if (home.name == username) { homeLeave(username); } sendmsg({ type: 0, msg: "用户<b>" + username + "</b>离开聊天室", counter: counter, users: users }) } });
Okay, you’re done.