您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關Nodejs如何實現多房間簡易聊天室功能的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
JS是JavaScript的簡稱,它是一種直譯式的腳本語言,其解釋器被稱為JavaScript引擎,是瀏覽器的一部分,主要用于web的開發,可以給網站添加各種各樣的動態效果,讓網頁更加美觀。
1、前端界面代碼
前端不是重點,夠用就行,下面是前端界面,具體代碼可到github下載。
2、服務器端搭建
本服務器需要提供兩個功能:http服務和websocket服務,由于node的事件驅動機制,可將兩種服務搭建在同一個端口下。
1、包描述文件:package.json,這里用到了兩個依賴項,mime:確定靜態文件mime類型,socket.io:搭建websocket服務,然后使用npm install 安裝依賴
{ "name": "chat_room", "version": "1.0.0", "description": "this is a room where you can chat with your friends", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "sfs", "license": "ISC", "dependencies": { "socket.io":"2.0.3", "mime":"1.3.6" } }
2、http服務器
http服務主要是給web瀏覽器提供靜態文件,既瀏覽器發來一個請求,服務器返回一個響應。
const http=require('http'), fs=require('fs'), path=require('path'), mime=require('mime'), chatServer=require('./lib/chat_server'); var cache={};//緩存靜態文件內容 //發送錯誤響應 function send404(response){ response.writeHead(404,{'Content-Type':'text/plain'}); response.write('Error 4.4:文件未找到。'); response.end(); } //發送文件內容 function sendFile(response,filePath,fileContents){ response.writeHead( 200, {"content-Type":mime.lookup(path.basename(filePath))} ); response.end(fileContents); } //查找文件 function serveStatic(response,cache,absPath){ if(cache[absPath]){ sendFile(response,absPath,cache[absPath]); }else{ fs.exists(absPath,function(exists){ if(exists){ fs.readFile(absPath,function(err,data){ if(err){ send404(response); }else{ cache[absPath]=data; sendFile(response,absPath,data); } }); }else{ send404(response); } }); } } //入口 var server=http.createServer(function(request,response){ var filePath=false; console.log(`new request for ${request.url}`); if(request.url==='/'){ filePath='public/index.html'; }else{ filePath='public'+request.url; } var absPath='./'+filePath; serveStatic(response,cache,absPath); }); server.listen(3000,function(){ console.log("the server is listening on prot 3000."); }); chatServer.listen(server); //websocket服務也綁定到該端口上
3、socket服務
socket.io提供了開箱既用的虛擬通道,所以不需要任務手動轉發消息到已連接的的用戶,可以使用 socket.broadcast.to(room).emit('message','hello'); room為某個聊天室id
const socketio=require('socket.io'); var io, guestNumber=1, //用戶編號 nickNames={}, //socket id對應的nickname namesUsed={}, //所有已使用的nickname allRooms={}, //聊天室--人數 currentRoom={}; //sockid--聊天室 module.exports.listen=function(server){ io=socketio.listen(server); io.serveClient('log level',1); io.sockets.on('connection',function(socket){ guestNumber=assignGuestName(socket,guestNumber,nickNames); joinRoom(socket,'Lobby'); handleMessageBroadcasting(socket,nickNames); handleNameChangeAttempts(socket,nickNames,namesUsed); handleRoomJoining(socket); socket.on('rooms',function(){ socket.emit('rooms',JSON.stringify(allRooms)); }); handleClientDisconnection(socket,nickNames,namesUsed); }); }; //新socket連入,自動分配一個昵稱 function assignGuestName(socket,guesetNumber,nickNames){ var name='Guest'+guestNumber; nickNames[socket.id]=name; socket.emit('nameResult',{ success:true, name:name }); namesUsed[name]=1; return guestNumber+1; } //加入某個聊天室 function joinRoom(socket,room){ socket.join(room); var num=allRooms[room]; if(num===undefined){ allRooms[room]=1; }else{ allRooms[room]=num+1; } currentRoom[socket.id]=room; socket.emit('joinResult',{room:room}); socket.broadcast.to(room).emit('message',{ text:nickNames[socket.id]+' has join '+room+'.' }); var usersinRoom=io.sockets.adapter.rooms[room]; if(usersinRoom.length>1){ var usersInRoomSummary='Users currently in '+room+' : '; for(var index in usersinRoom.sockets){ if(index!=socket.id){ usersInRoomSummary+=nickNames[index]+','; } } socket.emit('message',{text:usersInRoomSummary}); } } //修改昵稱 function handleNameChangeAttempts(socket,nickNames,namesUsed){ socket.on('nameAttempt',function(name){ if(name.indexOf('Guest')==0){ socket.emit('nameResult',{ success:false, message:'Names cannot begin with "Guest".' }); }else{ if(namesUsed[name]==undefined){ var previousName=nickNames[socket.id]; delete namesUsed[previousName]; namesUsed[name]=1; nickNames[socket.id]=name; socket.emit('nameResult',{ success:true, name:name }); socket.broadcast.to(currentRoom[socket.id]).emit('message',{ text:previousName+' is now known as '+name+'.' }); }else{ socket.emit('nameResult',{ success:false, message:'That name is already in use.' }); } } }); } //將某個用戶的消息廣播到同聊天室下的其他用戶 function handleMessageBroadcasting(socket){ socket.on('message',function(message){ console.log('message:---'+JSON.stringify(message)); socket.broadcast.to(message.room).emit('message',{ text:nickNames[socket.id]+ ': '+message.text }); }); } //加入/創建某個聊天室 function handleRoomJoining(socket){ socket.on('join',function(room){ var temp=currentRoom[socket.id]; delete currentRoom[socket.id]; socket.leave(temp); var num=--allRooms[temp]; if(num==0) delete allRooms[temp]; joinRoom(socket,room.newRoom); }); } //socket斷線處理 function handleClientDisconnection(socket){ socket.on('disconnect',function(){ console.log("xxxx disconnect"); allRooms[currentRoom[socket.id]]--; delete namesUsed[nickNames[socket.id]]; delete nickNames[socket.id]; delete currentRoom[socket.id]; }) }
3、客戶端實現socket.io
1、chat.js處理發送消息,變更房間,聊天命令。
var Chat=function(socket){ this.socket=socket;//綁定socket } //發送消息 Chat.prototype.sendMessage=function(room,text){ var message={ room:room, text:text }; this.socket.emit('message',message); }; //變更房間 Chat.prototype.changeRoom=function(room){ this.socket.emit('join',{ newRoom:room }); }; //處理聊天命令 Chat.prototype.processCommand=function(command){ var words=command.split(' '); var command=words[0].substring(1,words[0].length).toLowerCase(); var message=false; switch(command){ case 'join': words.shift(); var room=words.join(' '); this.changeRoom(room); break; case 'nick': words.shift(); var name=words.join(' '); this.socket.emit('nameAttempt',name); break; default: message='Unrecognized command.'; break; } return message; };
2、chat_ui.js 處理用戶輸入,根據輸入調用chat.js的不同方法發送消息給服務器
function divEscapedContentElement(message){ return $('<div></div>').text(message); } function divSystemContentElement(message){ return $('<div></div>').html('<i>'+message+'</i>'); } function processUserInput(chatApp,socket){ var message=$('#send-message').val(); var systemMessage; if(message.charAt(0)=='/'){ systemMessage=chatApp.processCommand(message); if(systemMessage){ $('#messages').append(divSystemContentElement(systemMessage)); } }else{ chatApp.sendMessage($('#room').text(),message); $('#messages').append(divSystemContentElement(message)); $('#messages').scrollTop($('#messages').prop('scrollHeight')); } $('#send-message').val(''); }
3、init.js客戶端程序初始化 創建一個websocket連接,綁定事件。
if(window.WebSocket){ console.log('This browser supports WebSocket'); }else{ console.log('This browser does not supports WebSocket'); } var socket=io.connect(); $(document).ready(function(){ var chatApp=new Chat(socket); socket.on('nameResult',function(result){ var message; if(result.success){ message='You are known as '+result.name+'.'; }else{ message=result.message; } console.log("nameResult:---"+message); $('#messages').append(divSystemContentElement(message)); $('#nickName').text(result.name); }); socket.on('joinResult',function(result){ console.log('joinResult:---'+result); $('#room').text(result.room); $('#messages').append(divSystemContentElement('Room changed.')); }); socket.on('message',function(message){ console.log('message:---'+message); var newElement=$('<div></div>').text(message.text); $('#messages').append(newElement); $('#messages').scrollTop($('#messages').prop('scrollHeight')); }); socket.on('rooms',function(rooms){ console.log('rooms:---'+rooms); rooms=JSON.parse(rooms); $('#room-list').empty(); for(var room in rooms){ $('#room-list').append(divEscapedContentElement(room+':'+rooms[room])); } $('#room-list div').click(function(){ chatApp.processCommand('/join '+$(this).text().split(':')[0]); $('#send-message').focus(); }); }); setInterval(function(){ socket.emit('rooms'); },1000); $('#send-message').focus(); $('#send-button').click(function(){ processUserInput(chatApp,socket); }); });
感謝各位的閱讀!關于“Nodejs如何實現多房間簡易聊天室功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。