This article shares the specific code of Vue+express+Socket to realize the chat function for your reference. The specific content is as follows Implementing chat functionSpecific functions Just to realize the function, without beautifying the interface 1. Enter the message and click Send. All users can receive the message below. 2. Enter the userid and click Connect to connect to the corresponding chat. In another interface, enter the userid of the page just now, enter the content and click Send to the designated person. Then the page just now can be printed out, but other pages will not be received, realizing the function of private chat. 3. The content display of private chat is not specifically implemented, but receiving and sending messages can be implemented. To implement the content display of private chat, you can add another private chat page Screenshots Project Preparation Only the socket preparation is introduced, and the construction of Vue and express is not introduced. Front-end socket Install npm i vue-socket.io --save Import import VueSocketIO from 'vue-socket.io' Background socket Install npm i socket.io --save Import Add to the bin/www folder produced by express-generator var io = require('socket.io')(server)' io.on('connection', (socket) => { socket.on('demining', (data) => { console.log(data); }); }); The specific screenshots are as follows: Project realizationVue Code HTML Code <div class="home"> userid: <input type="text" v-model="userid"> Nickname: <input type="text" v-model="name"> Message: <input type="text" v-model="msg" /> <button @click="send">Send</button> <button @click="join">Connect</button> <button @click="sendmsg">Send to specified person</button> <ul> <li v-for="(item,index) in chatList" :key="item.name + index"> {{ item.name }} says: {{ item.msg }} </li> </ul> </div> js code export default { name: "Home", data() { return { users: [], msg: "", chatList: [], name: '', userid: '' }; }, sockets: { // Connect to the background socket connect() { console.log('socket connected'); }, // User background call, add data sendMessage(data) { console.log(data); this.chatList.push(data) }, // User background call, print data receivemsg(data) { console.log('receivemsg'); console.log(data); } }, methods: { // Send a message to the background send() { // Use emit to call the message method in the background socket this.$socket.emit("message", { userid: 100, name: this.name, msg: this.msg }); }, // Establish user connection join() { this.$socket.emit("join", { userid: this.userid }); }, // Send a message to the backend for private messaging sendmsg() { this.$socket.emit("sendmsg", { userid: this.userid, msg: this.msg }); } } }; express code Add the following code to the connection defined in the www file just now // Used to store each user's socket to implement private chat function let arrAllSocket = {} // Create a socket connection io.on('connection', (socket) => { console.log('Connected'); // console.log(socket); // The join function is used by users to connect socket.on('join', function (obj) { console.log(obj.userid + 'join') // Save the connection status of each user for private messaging arrAllSocket[obj.userid] = socket }) // The function name for receiving messages sent by the foreground is message socket.on('message', (data) => { console.log(data); //Send the message back to the front desk (call the method defined in the front desk) The function name is sendMessage io.emit('sendMessage', data); }); // Private message socket.on('sendmsg', function (data) { console.log(data); // Query user connection let target = arrAllSocket[data.userid] if (target) { //Send a message to the specified person target.emit('receivemsg', data) } }) }) Background code encapsulationSince the www file should not have too much code, this part of the code is encapsulated 1. Create an io folder in the project directory with the following structure 2. Move the previous part of the code into io/index.js The code is as follows // Pass server as a parameter to module.exports = function (server) { var io = require('socket.io')(server); // Used to store each user's socket to implement private chat function let arrAllSocket = {} // Create a socket connection io.on('connection', (socket) => { console.log('Connected'); // console.log(socket); // The join function is used by users to connect socket.on('join', function (obj) { console.log(obj.userid + 'join') // Save the connection status of each user for private messaging arrAllSocket[obj.userid] = socket }) // The function name for receiving messages sent by the foreground is message socket.on('message', (data) => { console.log(data); //Send the message back to the front desk (call the method defined in the front desk) The function name is sendMessage io.emit('sendMessage', data); }); // Private message socket.on('sendmsg', function (data) { console.log(data); // Query user connection let target = arrAllSocket[data.userid] if (target) { //Send a message to the specified person target.emit('receivemsg', data) } }) }) } Finally, use the following code in the www file to introduce the file var io = require('../io') io(server) At this point, the basic functions of chatting are realized. Record it for later use. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL optimization strategy (recommended)
>>: Detailed instructions for installing Jenkins on Ubuntu 16.04
Operation effect html <head> <meta chars...
The specific method is as follows: CSS Code Copy ...
Nowadays, cross-platform development technology i...
Table of contents background 1. The query conditi...
Table of contents 1. The concept of process and t...
This article mainly introduces the configuration ...
Preface I have been summarizing my front-end know...
Effect: <!doctype html> <html> <he...
<br />Introduction: This idea came to me whe...
vue-infinite-scroll Install npm install vue-infin...
Anyone who has used Windows Remote Desktop to con...
wangEditor is a web rich text editor developed ba...
Written at the beginning I remember seeing a shar...
Copy code The code is as follows: <html> &l...
1.ssh command In Linux, you can log in to another...