使用nodejs引用socket.io做聊天室

Server:

 

var app = require('http').createServer(handler) , io = require('socket.io').listen(app) , fs = require('fs'); app.listen(80); console.log('server listen on port 80'); function handler (req, res) { fs.readFile(__dirname + '/public/index.html', function (err, data) { if (err) { res.writeHead(500); return res.end('Error loading index.html'); } res.writeHead(200); res.end(data); }); } var chatroom=io.of('/chat').on('connection',function(socket){ console.log(id+'connected'); var id=null; socket.on('setid',function(sid){ id=sid; socket.emit('id',id); chatroom.emit('msg',id+' join the chatroom'); }); socket.on('msg',function(msg){ chatroom.emit('msg',id+' : '+msg); }); })

Client:

   socket.io   

 

你可能感兴趣的:(Node.js,HTML5)