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)=='https://www.jb51.net/'){ 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); }); });
完整代码,可到https://github.com/FleyX/ChatRoom 下载。
以上所述是小编给大家介绍的Nodejs实现多房间简易聊天室功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章: