码迷,mamicode.com
首页 > Web开发 > 详细

NodeJS实现简单聊天室

时间:2014-12-18 10:17:39      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   sp   for   java   

转载:https://cnodejs.org/topic/53787247cbcc396349fe3f5a

npm install express --save

npm install socket.io --save

chat.js

var express = require(‘express‘);
var io = require(‘socket.io‘);
var app = express();
app.use(express.static(__dirname));
var server = app.listen(8888);
var ws = io.listen(server);
ws.on(‘connection‘, function(client) {
    console.log(‘someone is connect.\n‘);
    client.on(‘join‘, function(msg) {
        if (checkNickname(msg)) {
            client.emit(‘nickname‘, ‘昵称重复‘);
        } else {
            client.nickname = msg;
            ws.sockets.emit(‘announcement‘, ‘系统‘, msg + ‘加入了聊天室‘, {type: ‘join‘, name: getAllNickname()});
        }
    });
    client.on(‘send.message‘, function(msg) {
        client.broadcast.emit(‘send.message‘, client.nickname, msg);
    });
    client.on(‘disconnect‘, function() {
        if (client.nickname) {
            client.broadcast.emit(‘send.message‘, ‘系统‘, client.nickname + ‘离开聊天室‘);
        }
    });
});
var checkNickname = function(name) {
    for (var k in ws.sockets.sockets) {
        if (ws.sockets.sockets.hasOwnProperty(k)) {
            if (ws.sockets.sockets[k] && ws.sockets.sockets[k].nickname == name) {
                return true;
            }
        }
    }
    return false;
}
var getAllNickname = function() {
    var result = [];
    for (var k in ws.sockets.sockets) {
        if (ws.sockets.sockets.hasOwnProperty(k)) {
            result.push({name: ws.sockets.sockets[k].nickname});
        }
    }
    return result;
}

chat.html

<!DOCTYPE html>
<html>
<head>
<title>socket.io聊天室例子</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/bootstrap.css" />
<link rel="stylesheet" href="css/app.css" />
</head>
<body>
<div class="wrapper">
    <div class="content" id="chat">
        <ul id="chat_container">
        </ul>
    </div>
    <div class="action">
        <textarea></textarea>
        <button class="btn btn-success" id="clear">清屏</button>
        <button class="btn btn-success" id="send">发送</button>
    </div>
</div>
<script type="text/javascript" src="socket.io.js"></script>
<script type="text/javascript">
var ws = io.connect(http://127.0.0.1:8888);
var sendMsg = function(msg) {
    ws.emit(send.message, msg);
}
var addMessage = function(from, msg) {
    var li = document.createElement(li);
    li.innerHTML = <span> + from + </span> +  :  + msg;
    document.querySelector(#chat_container).appendChild(li);
    document.querySelector(#chat).scrollTop = document.querySelector(#chat).scrollHeight;
    document.querySelector(textarea).focus();
}
var send = function() {
    var ele_msg = document.querySelector(textarea);
    var msg = ele_msg.value.replace(\r\n, ‘‘).trim();
    //console.log(msg);
    if (!msg) return;
    sendMsg(msg);
    addMessage(, msg);
    ele_msg.value = ‘‘;
}
ws.on(connect, function() {
    var nickname = window.prompt(输入你的昵称);
    while (!nickname) {
        nickname = window.prompt(昵称不能为空);
    }
    ws.emit(join, nickname);
});
ws.on(nickname, function() {
    var nickname = window.prompt(昵称重复);
    while (!nickname) {
        nickname = window.prompt(昵称不能为空);
    }
    ws.emit(join, nickname);
});
ws.on(send.message, function(from, msg) {addMessage(from, msg);});
ws.on(announcement, function(from, msg) {addMessage(from, msg);});
document.querySelector(textarea).addEventListener(keypress, function(event) {
    if (event.which == 13) {
        send();
    }
});
document.querySelector(textarea).addEventListener(keydown, function(event) {
    if (event.which == 13) {
        send();
    }
});
document.querySelector(#send).addEventListener(click, function() {
    send();
});
document.querySelector(#clear).addEventListener(click, function() {
    document.querySelector(#chat_container).innerHTML = ‘‘;
});
</script>
</body>
</html>

 

NodeJS实现简单聊天室

标签:style   blog   http   ar   io   color   sp   for   java   

原文地址:http://www.cnblogs.com/feilv/p/4170981.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!