2016-1108
websocket
- 原生socket
- Net下socket
- server端
var net = require('net');
var server = net.createServer((socket) => {
//添加客户端到列表
var name = clients.add(socket);
//发送数据监听
socket.on('data', (data) => {
console.log(data.toString());
clients.sendAll(socket, data);
});
//关闭监听
socket.on('close', (data) => {
console.log(data);
})
});
var net = require('net');
//链接到服务端
var client = net.connect({port:8000}, () => {
console.log('链接成功');
})
//发送数据
client.on('data', (data) => {
console.log(data.toString());
})
//关闭连接
client.on('end', () => {
console.log('断开了');
})
- express 使用socket eg: socket.io
- npm install socket.io