一》socketIo:socketIo是node.js中的一个模块,提供通过websocket进行通讯的一种简单方式。
websocket:服务器和客户端之间实现实时通讯的响应方式。基本思想是:在服务器和客户端之间保持连接持久打开。websocket不支持重新连接处理。
二》socketIo的简单实用:
要想在服务器端加入socketIo的功能,必须将Socket.Io包括进来,而后将其附加到服务器上,如:
var io=require('socket.io').listen(server);
io.socket.on('connection',function(socket){
console.log("user connected");
socket.on('disconnection',function(){
console.log("user disconnected");
});
});
这时,会把socketIo绑定到服务器上,具备了和客户端交互的功能。SocketIo可以监听到许多时间,比如客户端的连接和断开连接。
2)客户端连接到服务器
为了让浏览器可以连接到服务器,那么客户端就必须使用Socket.Io中低的客户端JavaScript库,然后连接到服务器,如:
<script src="/socket.io/socket.io.js"></script>
<script>var socket=io.connect('http://127.0.0.1:3000')</script>
3)测试:
app.js(服务器段)
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
fs.readFile('./index.html', function(error, data) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data, 'utf-8');
});
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
console.log('User connected');
socket.on('disconnect', function () {
console.log('User disconnected');
});
});
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Socket.IO Example</title>
</head>
<body>
<h1>Socket.IO Example</h1>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://127.0.0.1:3000');
</script>
</body>
</html>
测试结果如下:
D:\nodejs\socketTest>node app.js
Server running at http://127.0.0.1:3000/
info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 13729343131701654457
debug - setting request GET /socket.io/1/websocket/13729343131701654457
debug - set heartbeat interval for client 13729343131701654457
debug - client authorized for
debug - websocket writing 1::
User connected
debug - emitting heartbeat for client 13729343131701654457
debug - websocket writing 2::
debug - set heartbeat timeout for client 13729343131701654457
debug - got heartbeat packet
debug - cleared heartbeat timeout for client 13729343131701654457
debug - set heartbeat interval for client 13729343131701654457
debug - client authorized
info - handshake authorized 10810714391399389314
debug - setting request GET /socket.io/1/websocket/10810714391399389314
debug - set heartbeat interval for client 10810714391399389314
debug - client authorized for
debug - websocket writing 1::
User connected
info - transport end
debug - set close timeout for client 10810714391399389314
debug - cleared close timeout for client 10810714391399389314
debug - cleared heartbeat interval for client 10810714391399389314
User disconnected
debug - discarding transport
debug - emitting heartbeat for client 13729343131701654457
debug - websocket writing 2::
debug - set heartbeat timeout for client 13729343131701654457
debug - got heartbeat packet
debug - cleared heartbeat timeout for client 13729343131701654457
debug - set heartbeat interval for client 13729343131701654457
websocket:服务器和客户端之间实现实时通讯的响应方式。基本思想是:在服务器和客户端之间保持连接持久打开。websocket不支持重新连接处理。
二》socketIo的简单实用:
要想在服务器端加入socketIo的功能,必须将Socket.Io包括进来,而后将其附加到服务器上,如:
var io=require('socket.io').listen(server);
io.socket.on('connection',function(socket){
console.log("user connected");
socket.on('disconnection',function(){
console.log("user disconnected");
});
});
这时,会把socketIo绑定到服务器上,具备了和客户端交互的功能。SocketIo可以监听到许多时间,比如客户端的连接和断开连接。
2)客户端连接到服务器
为了让浏览器可以连接到服务器,那么客户端就必须使用Socket.Io中低的客户端JavaScript库,然后连接到服务器,如:
<script src="/socket.io/socket.io.js"></script>
<script>var socket=io.connect('http://127.0.0.1:3000')</script>
3)测试:
app.js(服务器段)
var http = require('http');
var fs = require('fs');
var server = http.createServer(function (req, res) {
fs.readFile('./index.html', function(error, data) {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data, 'utf-8');
});
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
console.log('User connected');
socket.on('disconnect', function () {
console.log('User disconnected');
});
});
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Socket.IO Example</title>
</head>
<body>
<h1>Socket.IO Example</h1>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://127.0.0.1:3000');
</script>
</body>
</html>
测试结果如下:
D:\nodejs\socketTest>node app.js
Server running at http://127.0.0.1:3000/
info - socket.io started
debug - served static content /socket.io.js
debug - client authorized
info - handshake authorized 13729343131701654457
debug - setting request GET /socket.io/1/websocket/13729343131701654457
debug - set heartbeat interval for client 13729343131701654457
debug - client authorized for
debug - websocket writing 1::
User connected
debug - emitting heartbeat for client 13729343131701654457
debug - websocket writing 2::
debug - set heartbeat timeout for client 13729343131701654457
debug - got heartbeat packet
debug - cleared heartbeat timeout for client 13729343131701654457
debug - set heartbeat interval for client 13729343131701654457
debug - client authorized
info - handshake authorized 10810714391399389314
debug - setting request GET /socket.io/1/websocket/10810714391399389314
debug - set heartbeat interval for client 10810714391399389314
debug - client authorized for
debug - websocket writing 1::
User connected
info - transport end
debug - set close timeout for client 10810714391399389314
debug - cleared close timeout for client 10810714391399389314
debug - cleared heartbeat interval for client 10810714391399389314
User disconnected
debug - discarding transport
debug - emitting heartbeat for client 13729343131701654457
debug - websocket writing 2::
debug - set heartbeat timeout for client 13729343131701654457
debug - got heartbeat packet
debug - cleared heartbeat timeout for client 13729343131701654457
debug - set heartbeat interval for client 13729343131701654457