Node.js:0.10.31
在e:/nodejs/新建文件夹easychat
在easychat/下新建app.js 和 index.html
app.js:
var fs = require('fs')
, http = require('http')
, socketio = require('socket.io');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-type': 'text/html'});
res.end(fs.readFileSync(__dirname + '/index.html'));
}).listen(3000, function() {
console.log('Listening at: http://localhost:3000');
});
socketio.listen(server).on('connection', function (socket) {
socket.on('message', function (msg) {
console.log('Message Received: ', msg);
socket.broadcast.emit('message', msg);
});
});
index.html:
<html>
<head>
<script src="http://cdn.staticfile.org/jquery/2.1