In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to implement a multi-person chat room with js code. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Design requirements:
1) users should log in to the chat room by registering
2) chat rooms can display all online users
3) display the user name that sent the chat before each chat.
4) you can have a private chat.
5) when the user enters and leaves the chat room, the system will broadcast in the chat room
The config.js code is as follows
Module.exports= {"port": 3000, "host": "127.0.0.1"}
The broadcast.js code is as follows
Exports.broadcast=function (data,users) {var from=data.from; var message=data.message; message= from+ "says:" + message; / / build message var send= {mstype: "broadcast", message:message}; send= new Buffer (JSON.stringify (send)) / / traverse all users of the user group, all users of the originating party for (var username in users) {if (username users from) {users [username] .write (send);}
The Signup.js code is as follows
Exports.signup = function (socket,data,users) {/ / get the user name of the registered user var username=data.username; if (! users [username]) {/ / does not exist, save the user name and socket users [username] = socket; var send= {mstype: "signup", code:1000, username:username, message: "registered successfully"} Socket.write (JSON.stringify (send));} else {/ / cunzai var send= {mstype: "signup", code:1001, message: "user name has been occupied, please re-enter user name"} socket.write (JSON.stringify (send));}}
The p2p.js code is as follows
Exports.p2p=function (socket,data,users) {var from=data.from; var to=data.to; var message=data.message; var receiver=users [to]; if (! receiver) {/ / recipient var send= {mstype: "P2P", code:2001, message: "user" + to+ "does not exist"} socket.write (JSON.stringify (send)) } else {/ / send message var send= {mstype: "P2P", code:2000, from:from, message:from+ "to you" + message} receiver.write (JSON.stringify (send));}}
Server server-side code
/ / P2P chat room server var net=require ("net"); var config=require (". / config"); var broadcast=require (". / broadcast"); var p2p=require (". / P2P"); var signup=require (". / signup"); var users= {}; var server=net.createServer (); server.on ("connection", function (socket) {socket.on ("data", function (data) {data = JSON.parse (data)) Switch (data.mstype) {case "signup": signup.signup (socket, data, users); break; case "broadcast": broadcast.broadcast (data, users); break; case "P2P": p2p.p2p (socket, data, users); break Default: break;}}); socket.on ("error", function () {console.log ("client abnormally exited");}); server.listen (config.port,config.host,function () {console.log ("server in port" + config.port+ "start listening");})
The Client client code is as follows:
Var net=require ("net"); var config=require (". / config"); var Client=net.createConnection ({port:config.port, host:config.host}); var username;Client.on ("connect", function () {console.log ("Please enter user name:"); process.stdin.on ("data", function (data) {data=data.toString (). Trim (); / / determine whether the user already exists if (! Username) {var send= {mstype: "signup", username:data}; Client.write (JSON.stringify (send)); return;} var regex=/ (. {1J 18}): (. +) /; var matches=regex=regex.exec (data) If if (matches) {/ / can match, the sender of P2P var from=username;// is its own var to=matches [1]; / / to whom var message=matches [2] / / construct JSON form information var send= {mstype: "P2P", from:username, to:to, message:message}; Client.write (JSON.stringify (send)) } else {/ / broadcast var send= {mstype: "broadcast", from:username, message:data}; Client.write (JSON.stringify (send);}});}); Client.on ("data", function (data) {data=JSON.parse (data)) Switch (data.mstype) {case "signup": var code=data.code; switch (code) {case 1000: username=data.username; console.log (data.message); break; case 1001: console.log (data.message) Break; default: break;} break; case "broadcast": console.log (data.message); break; case "P2P": var code=data.code Switch (code) {case 2000: console.log (data.message); break; case 2001: console.log (data.message); break; default: break } break; default: break;}); Client.on ("error", function () {console.log ("chat room is closed!") (}) this is the end of the article on "how to implement a multi-person chat room with js code". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.