var express = require('express'),
io = require('socket.io');
var app = express();
app.use(express.static(__dirname));
var server = app.listen(8888);
var ws = io.listen(server);
<!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_conatiner">
</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="js/socket.io.js"></script>
<script type="text/javascript">
var ws = io.connect('http://172.16.2.184: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_conatiner').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('rn', '').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);
}); www.jb200.com
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_conatiner').innerHTML = '';
});
</script>
</body>
</html>