轮流使用socket.io&的node.js

时间:2017-12-15 09:03:16

标签: jquery node.js socket.io

我正在尝试制作一款基于回合制的游戏,但我无法弄清楚我做错了什么。

我似乎无法为特定用户禁用按钮,即当不是那些人转动时,他们不应该被允许做任何事情,在这种情况下禁用绘图和支架按钮。如何根据此转弯进行其他客户端的禁用?

使用我当前的代码,任何客户端都不会禁用这些按钮,并且它们总是可以播放而不是轮流播放。

服务器端

var express = require('express')
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

var users = [];
var index = 0;

io.on('connection', function(socket) {
socket.on('add-user', function(data) {
   users = [{
  'name': data,
  'socket': socket.id,
  'total': 0,
 }];
});

socket.on('game-start', function () { 
  if(users[index].socket === socket.id) {
    console.log(users[index].socket);
    console.log(socket.id);
    users[index].name.turn = true;
    socket.emit('enable-button');
    socket.on('draw', function() {
    ... game logic here ...
    });
    socket.on('stand-button', function() {
        // move to next player.
        if (users[index+1] != null) {
          index += 1;
          console.log(index);
        }
    });
   }
});

socket.on('disconnect', function() {
  for(var i = 0; i < users.length; i++) {
    if(users[i].socket === socket.id) {
      io.emit('chat message', users[i].name + ' has rage quit!');
        break;
    }
  } 
});
});

客户端

$('#stand').click(function() {
   socket.emit('stand-button');
   $('#draw').prop("disabled", true);
   $('#stand').prop("disabled", true);
});

socket.on('enable-button', function() {
  $('#draw').prop("disabled", false);
  $('#stand').prop("disabled", false);
});

0 个答案:

没有答案