基本信息
源码名称:websocket记录页面的停留时间(ip/进入页面时间/离开时间/访客时长等)
源码大小:4.55M
文件格式:.zip
开发语言:js
更新时间:2019-04-09
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
使用nodejs中的socket.io实现的websocket连接。

前端界面只需引用如下脚本即可:

数据库统计到的结果如下:


const Sequelize = require('sequelize')
var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io')(server),
    port = process.env.PORT || 8000,
    sequelize = new Sequelize('mysql://root:root@localhost:3306/visitordb');
const User = sequelize.define('visitlog', {
    socketId: {type: Sequelize.STRING, allowNull: false, primaryKey: true},
    ip: {type: Sequelize.STRING, allowNull: false},
    area: {type: Sequelize.STRING, allowNull: false},
    enterTime: {type: Sequelize.DATE, allowNull: false},
    leaveTime: {type: Sequelize.DATE, allowNull: true},
    cookieId: {type: Sequelize.STRING, allowNull: false},
}); // 时间戳默认为 false
app.use(express.static(__dirname   '/client'));//通过 express 加载前端部分的静态资源文件
io.on('connection', (socket) => {
    let user = '游客'   socket.id, area = '';
    let ip = (socket.handshake.headers['x-forwarded-for'] != null) ? socket.handshake.headers['x-forwarded-for'] : socket.handshake.address;
    User.create({socketId: socket.id, ip: ip, area: area, enterTime: new Date().toUTCString(), leaveTime: null, cookieId: socket.handshake.query.cookieId});
    //sequelize.sync().then(() => {onsole.log('init db ok')}).catch(err => {console.log('init db error', err)});
    console.log(user   '进来了');
    //通知用户离开
    socket.on('disconnect', () => {
        sequelize.query('update visitlogs set leaveTime=NOW() WHERE socketId = :socketId ', {replacements: {socketId: socket.id}, type: sequelize.QueryTypes.UPDATE}
        ).then(projects => {
            console.log(projects)
        })
        console.log(user   '离开了');
    });
});

server.listen(port, () => {
    console.log('listening on %d...', port);
});