V
V
Vladimir2019-04-24 02:20:17
Node.js
Vladimir, 2019-04-24 02:20:17

How to synchronize action for socket.io clients?

Here is the client code:

var host = 'http://localhost:3000/',socket = io(host),hp,hp2,connections,x;

      		
$('.boss').click(function() {
     socket.emit('message',54397);
     $('.hp-b').css("width", x+'%');
     alert(x);
});	
socket.emit('forceDisconnect');

socket.on('connections', function(data) {
  connections = data;
  $('.users-count').html(connections);	
});

socket.on('new message', function(data) {
  hp = data;
  $('.hp-b').html(hp);
});

$('.hp-b').html(hp);
      	       	 
socket.on('disconnect', function(data) {
  alert('Сервер заполнен'); 
 // window.location.replace(host + 'serverfull');
});
socket.on('w', function(data) {
  x = data;
});


window.onbeforeunload = function() { 
  //return "Данные не сохранены. Точно перейти?";
  
};

Here are the servers:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var connections=0,max_connections=2,port = 3000,maxhp = 300000,hp = maxhp,n = maxhp;

let io = require('socket.io')(http);

app.use(express.static(__dirname + '/client'));

app.get('/go', function(req,res) {
  res.sendFile(__dirname + '/client/public/index.html');
});

http.listen(port, function() {
  console.log('listening on *:' + port);
});

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);
      });
      
      
      
      
      socket.on('message', function(data) {
          hp = hp-data;
          if(hp<=0) hp = 0;
          x = hp/n*100;
   			io.emit('new message', hp);
   			if(hp>0) io.emit('w', x);
   			if(hp<=0) io.emit('w', 0);
   			console.log(hp);
   	});
   	x = hp/n*100;
   	io.emit('w', x);
   	if(hp<=0) hp = 0, x=-100;
   	
   	socket.on('hp', function(data) {
    	io.emit('hp2', n);
   		console.log(data);
   	});
   	
   	
});

Hp works, if I do damage from one client, then when the 2nd client is connected, it will show him the current hp, and if they have already been connected, hp will be taken away evenly on all clients.
Wrote a strip script with xp, inserted the same code as on xp, it doesn't work, help me ^^

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2019-04-24
@HistoryART


Here is the client 's response :

var host = 'http://localhost:3000/',socket = io(host),hp,hp2,connections,x;

      		
$('.boss').click(function() {
     socket.emit('damage',54397);
     $('.hp-line').css("width", x+'%');
});	
socket.emit('hp line for all');
socket.emit('forceDisconnect');

socket.on('connections', function(data) {
  connections = data;
  $('.users-count').html(connections);	
});

socket.on('realtime hp', function(data) {
  hp = data;
  $('.hp-line').html(hp);
});

$('.hp-line').html(hp);
      	       	 
socket.on('disconnect', function(data) {
  alert('Сервер заполнен'); 
 // window.location.replace(host + 'serverfull');
});
socket.on('hp line width', function(data) {
  x = data;
  $('.hp-line').css("width", x+'%');
});


window.onbeforeunload = function() { 
  //return "Данные не сохранены. Точно перейти?";
  
};

Server:
var app = express();
var http = require('http').Server(app);
var connections=0,max_connections=2,port = 3000,maxhp = 300000,hp = maxhp,x;

let io = require('socket.io')(http);

app.use(express.static(__dirname + '/client'));

app.get('/go', function(req,res) {
  res.sendFile(__dirname + '/client/public/index.html');
});

http.listen(port, function() {
  console.log('listening on *:' + port);
});

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);
      });
      
      
      
      
      socket.on('damage', function(data) {
          hp = hp-data;
          if(hp<=0) hp = maxhp;
          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);
   			console.log(hp);
   	});
   	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);
   	});
   	
   	
});
var express = require('express');

A
Alexander Aksentiev, 2019-04-24
@Sanasol

depends on what needs to be done.
Now you have porridge.
It is not clear hp in general to whom it belongs and from whom it is taken away.
It is necessary to take out on the server in the global area if this is the same number for all.
Or make the database a global variable of clients, each with its own value and, accordingly, take it away from each specific client individually.
Your code is not even close to either option.
It is necessary to redo all the logic completely.
Here you can’t even sketch out a working version in the answer by hand.
Here is an example of a working shooting game without hp, but with exp that increases if one player hits another.
https://github.com/S-anasol/network/blob/master/se...
In general, the code is large right off the bat, nothing will be clear, most likely, but at least a working approximate version.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question