Сообщество IT-специалистов
Ответы на любые вопросы об IT
Профессиональное развитие в IT
Удаленная работа для IT-специалистов
Пытаюсь через localstorage, но после перезагрузки всё пропадает,
единственное что работает, хранение на сервере, но если я буду хранить там,то эти данные раздадутся на всех,
var express = require('express'); var app = express(); var http = require('http').Server(app); var mysql = require('mysql'); var session = require("express-session")({ secret: 'boss', resave: true, saveUninitialized: true }); var sharedsession = require("express-socket.io-session"); var connections=0,max_connections=5,port = 3000,maxhp=300000,hp=maxhp,x,msg,db; var max_user_hp,user_hp,x2,email,nick; var hour = 3600000; let io = require('socket.io')(http, { }); app.use(express.static(__dirname + '/client')); app.use(session); // Use shared session middleware for socket.io // setting autoSave:true io.use(sharedsession(session, { autoSave:true })); app.get('/', function(req,res) { res.sendFile(__dirname + '/client/public/auth.html'); }); app.get('/boss', function(req,res) { res.sendFile(__dirname + '/client/public/boss.html'); }); http.listen(port, function() { console.log('listening on *:' + port); }); db = mysql.createConnection({ host : 'localhost', user : 'root', password : '', database : 'game' }); db.connect((err) => { if(err) console.log(err); console.log("Mysql connected"); }); io.on('connection', function(socket) { //Контроль подключений connections+=1; io.emit('connections', connections); console.log('connected client:' + connections); if(connections > max_connections) { socket.on('forceDisconnect', () => { socket.disconnect(true); console.log('Server full!' + max_connections + '/' + max_connections); }); } socket.on( 'disconnect', () => { connections -= 1; console.log( 'client disconnected:' + connections); io.emit('connections', connections); io.emit('server full', 'Вы отключены от сервера!'); }); //io.set("reconnection limit", 5000); //end //Хп босса socket.on('boss fight', function(data) { hp = hp-data; if(hp<=0) { hp = maxhp; io.emit('win'); } x = hp/maxhp*100; io.emit('realtime hp', hp); if(hp>0) io.emit('hp line width', x); if(hp<=0) io.emit('hp line width', 0); if(hp+user_hp <= 0) { io.emit('draw'); } }); socket.on('hp line for all', function(data) { x = hp/maxhp*100; io.emit('realtime hp', hp); io.emit('hp line width', x); if(hp<=0) { io.emit('hp line width', 0); io.emit('win'); } if(hp+user_hp <= 0) { io.emit('draw'); } }); //end socket.handshake.session.maxhp = 300000; max_user_hp = socket.handshake.session.maxhp; socket.handshake.session.hp = max_user_hp; user_hp = max_user_hp; //Хп игрока socket.on('user fight', function(data) { user_hp = user_hp-data; if(user_hp<=0) { user_hp = max_user_hp; socket.emit('lose'); } x2 = user_hp/max_user_hp*100; socket.emit('user hp', user_hp); if(user_hp>0) socket.emit('user hp line width', x2); if(user_hp<=0) socket.emit('user hp line width', 0); if(hp+user_hp <= 0) { socket.emit('draw'); } }); //io.sockets.connected[socket.id].emit('class', 'alert'); socket.on('hp line for all', function(data) { x2 = user_hp/max_user_hp*100; socket.emit('user hp line width', x2); if(user_hp<=0) { socket.emit('user hp line width', 0); socket.emit('lose'); } if(hp+user_hp <= 0) { socket.emit('draw'); } }); //end //Чат socket.on('chat message', function(data) { msg = data; io.emit('chat msg',msg); }); //end //Блокировка пользователей socket.on('banned', function(data) { var ban = ''; if(data == ban) { io.emit('ban', 'Вы забанены!'); socket.disconnect(true); } }); //end //Регистрация socket.on('email', function(data) { email = data; }); socket.on('nick', function(data) { nick = data; console.log(data); }); socket.on('query', function(data) { db.query("INSERT INTO players (id,nickname,email) VALUES('','" + nick + "','" + email + "')", (err,result) => { if(err) console.log(err); console.log("Insert"); }); }); //end });