在当前项目(socket文件夹下)进行快速开始的例子。 #### 安装express ~~~ npm install --save express@4.15.2 ~~~ #### 创建io.js文件 代码如下 ~~~ var app = require('express')(); var http = require('http').Server(app); app.get('/', function(req, res) { res.send('<h1>Hello world</h1>'); }); http.listen(3000, function() { console.log('listening on *:3000'); }); ~~~ 输入 `node io.js` 运行程序 打开浏览器访问:http://127.0.0.1:3000/ 输出 `Hello world` 则代表运行成功。 #### 新建io.html文件 代码如下 ~~~ <!doctype html> <html> <head> <title>Socket.IO chat</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font: 13px Helvetica, Arial; } form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } #messages { list-style-type: none; margin: 0; padding: 0; } #messages li { padding: 5px 10px; } #messages li:nth-child(odd) { background: #eee; } </style> </head> <body> <ul id="messages"></ul> <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> </body> </html> ~~~ #### 修改io.js代码 ~~~ app.get('/', function(req, res) { res.sendFile(__dirname + '/index.html'); }); ~~~ 停止运行io.js。 按` ctrl+c` 输入 `node io.js` 重新运行程序 打开浏览器访问:http://127.0.0.1:3000/ 没报错证明代码没问题。 #### 安装`socket.io` ~~~ npm install --save socket.io ~~~ #### 修改io.js文件代码 ~~~ var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function(req, res) { res.sendFile(__dirname + '/io.html'); }); io.on('connection', function(socket) { console.log('a user connected'); }); http.listen(3000, function() { console.log('listening on *:3000'); }); ~~~ #### 修改io.html文件代码 在`</body>`前加以下代码 ~~~ <script src="/socket.io/socket.io.js"></script> <script> var socket = io(); </script> ~~~ 完整代码如下 ~~~ <!doctype html> <html> <head> <title>Socket.IO chat</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font: 13px Helvetica, Arial; } form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } #messages { list-style-type: none; margin: 0; padding: 0; } #messages li { padding: 5px 10px; } #messages li:nth-child(odd) { background: #eee; } </style> </head> <body> <ul id="messages"></ul> <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> <script src="/socket.io/socket.io.js"></script> <script> var socket = io(); </script> </body> </html> ~~~ 重新运行io.js,并刷新页面,可以在控制台(node.js控制台)看到`a user connected`的相关信息。 #### 修改io.js文件代码 ~~~ io.on('connection', function(socket){ console.log('a user connected'); socket.on('disconnect', function(){ console.log('user disconnected'); }); }); ~~~ 重新运行io.js,并刷新页面,可以在控制台(node.js控制台) ~~~ listening on *:3000 a user connected user disconnected a user connected ~~~ #### 修改io.js文件代码 ~~~ io.on('connection', function(socket) { console.log('a user connected'); socket.on('disconnect', function() { console.log('user disconnected'); }); socket.on('chat message', function(msg) { console.log('message: ' + msg); }); }); ~~~ #### 修改io.html文件代码 ~~~ <script src="/socket.io/socket.io.js"></script> <script src="https://code.jquery.com/jquery-1.11.1.js"></script> <script> var socket = io(); $('form').submit(function() { socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); </script> ~~~ 重新运行io.js,并刷新页面,然后在输入框中输入222222222222222,点击send按钮,可以在控制台(node.js控制台) ~~~ listening on *:3000 a user connected user disconnected a user connected message: 222222222222222 ~~~ #### 修改io.js文件代码 ~~~ socket.on('chat message', function(msg) { console.log('message: ' + msg); io.emit('chat message', msg); }); ~~~ #### 修改io.html文件代码 ~~~ <script> var socket = io(); $('form').submit(function() { socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg) { $('#messages').append($('<li>').text(msg)); }); </script> ~~~ 重新运行io.js,并刷新页面,然后在输入框中输入222222222222222,点击send按钮,可以在浏览器界面看到聊天信息。