js代码实现多人聊天室

【js代码实现多人聊天室】本文实例为大家分享了js代码实现多人聊天室的具体代码,供大家参考,具体内容如下
设计要求: 1)用户应通过注册登录聊天室
2)聊天室可以显示所有在线用户
3)在每一条聊天内容之前,显示发出这条聊天内容的用户名。
4)可进行私密聊天。
5)当用户进入和离开聊天室时,系统会在聊天室里广播
config.js 代码如下

module.exports={"port":3000,"host":"127.0.0.1"}

broadcast.js 代码如下
exports.broadcast=function (data,users) {var from=data.from; var message=data.message; message = from+"说: "+message; //构建消息var send={mstype:"broadcast",message:message}; send =new Buffer(JSON.stringify(send)); //遍历用户组所有用户,出发送方的所有用户for(var username in users){if(username!=from){users[username].write(send); }}};

Signup.js 代码如下
exports.signup = function (socket,data,users) {//获取注册用户的用户名var username=data.username; if(!users[username]){//不存在,则保存用户名和socketusers[username]=socket; var send={mstype:"signup",code:1000,username:username,message:"注册成功"}; socket.write(JSON.stringify (send)); }else{//cunzaivar send={mstype:"signup",code:1001,message: "用户名已被占,请重新输入用户名"}socket.write(JSON.stringify(send)); }};

p2p.js 代码如下
exports.p2p=function (socket,data,users) {var from=data.from; var to=data.to; var message=data.message; var receiver=users[to]; if(!receiver){//接收方不存在var send={mstype:"p2p",code:2001,message:"用户"+to+"不存在"}socket.write(JSON.stringify(send)); }else{//存在则向接收方发送信息var send={mstype:"p2p",code:2000,from:from,message:from+"对你说"+message}receiver.write(JSON.stringify(send)); }};

Server服务器端代码
//p2p 聊天室服务器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 = https://www.it610.com/article/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("有客户端异常退出了"); }); }); server.listen(config.port,config.host,function () {console.log("服务器在端口"+config.port+"启动监听"); });

Client客户端代码如下:
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("请输入用户名:"); process.stdin.on("data",function (data){data=https://www.it610.com/article/data.toString().trim(); //判断用户是否已经存在if(! username){var send={mstype:"signup",username:data}; Client.write(JSON.stringify(send)); return; }var regex=/(.{1,18}):(.+)/; var matches=regex=regex.exec(data); if(matches){//能匹配则是p2pvar from=username; //发送方是自己var to=matches[1]; //发给谁var message=matches[2]; //构造JSON形式信息var send={mstype: "p2p",from:username,to:to,message:message}; Client.write(JSON.stringify(send)); }else{//广播var send={mstype:"broadcast",from:username,message:data}; Client.write(JSON.stringify(send)); }}); }); Client.on("data",function (data) {data=https://www.it610.com/article/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("聊天室已关闭!!"); })

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

    推荐阅读