jquery - Node js setInterval For Two Players -


i developing game in node.js , socket.io , jquery.at main time 2 users logging site different location , different browser. when first user login play game message should come "your turn " , play 30 sec. in turn. after 30 sec. turn goes second user logged in different location "your turn " after completion of 30 sec. of second user turn goes first user , process continue .

this app.js in node.js

 var express = require('express')     , app = express()     , http = require('http')     , server = http.createserver(app)     , io = require('socket.io').listen(server);     server.listen(8080);     app.get('/', function (req, res) {     res.sendfile(__dirname + '/index.html');     });    var users = {};      io.sockets.on('connection', function(socket){     socket.on('adduser', function(user){     socket.user = user;     users[user] = user;     socket.emit('updateuser',     socket.user + ' has connected game');     });     socket.on('playgame1', function(data){     io.sockets.emit('update',  data);     });     }); 

and index.html

<html>     <head>         <title>game development in node.js</title>         <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>         <script type="text/javascript" src="/socket.io/socket.io.js"></script>         <script type="text/javascript">         var socket = io.connect('http://localhost:8080');         //var left = {'left': '-=100'};         socket.on('connect', function(){                 socket.emit('adduser', prompt('please enter username'));         });          socket.on('update',function (data){             console.log(data);             $('#arrow').css('left',data.left);             $('#arrow').css('top',data.top);             if(data.top == 50){                 alert("you won");             }         });          socket.on('updateuser', function(user){                      $('#displayuser').append('<b>'+ user +'</b>');           });      //  var auto_refresh = setinterval(function (){                     //          $('#loadpage').load( alert('your turn') );                     //      }, 20000);               $(document).ready(function(){                 $(document).keydown(function(e) {                     if(e.which == 37){                         $('#arrow').css({'left': '-=100'});                     }                     if(e.which == 38){                         $('#arrow').css({ 'top': '-=100' });                     }                     if(e.which == 39){                         $('#arrow').css({ 'left': '+=100' });                     }                     if(e.which == 40){                         $('#arrow').css({ 'top': '+=100' });                                             }                     //e.preventdefault(); // prevent default action (scroll / move caret)                      var data = {left:$('#arrow').position().left,top:$('#arrow').position().top};                                 socket.emit('playgame1', data);                 });             });         </script>         <style>             #arrowcontainer{                 height: 500px;                 width: 500px;                 border: 1px solid #000;                 margin: 0px auto;                 padding: 0px;                 position: relative;                 overflow: hidden;             }             #arrow{                 height: 10px;                 width: 10px;                 margin: 0px auto;                 padding: 0px;                 position: absolute;                 top: 250px;                 left: 0;                 bottom: 0;                 right: 0;             }             #displayuser{                 float: none;                 margin: 0 auto;                 width: 500px;                 height: auto;             }             #result{                 height: 10px;                 width: 10px;                 margin: 0px auto;                 padding: 0px;                 position: absolute;                 top: 50px;                 left: 0;                 bottom: 0;                 right: 0;             }         </style>     </head>     <body id="loadpage">         <div id="displayuser"></div>         <div id="arrowcontainer">             <div id="result">+</div>             <div id="arrow">*</div>         </div>     </body> </html> 

so please tell me how setinterval 2 players. here code.when creating call goes @ same time both users dont want that.its full running code can check it.

thanks raghvendra

some things consider description. these ideas build app - code run more.

consider web socket "channel" each client (and @ least each game). perhaps room or namespace socket.io docs. allow send message right client. alternatively, filter on client side identifier e.g. "this message id=player1". both message, , client code shows when matters.

setinterval defined on server side. need pergame, , cancel internal when game ends. globally on server (just consider processing when internal runs, if have many games...). simple example, use node's native call setinterval

function changeturns(){    //check server message send.    //check server web socket send message    //send message }  setinterval( function(){   changeturns(); }, 30000 ); 

the server should track whos turn is. maybe client disable actions when not turn, better not allow actions when not turn server. need send message both clients, 1 knows turn over, , other knows turn has begun. single message parameter checked on client side, or 2 different messages turnover , turnstarted sent respective clients.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -