2012-09-03 2 views
0

현재 Construct2에서 게임을하고 있습니다. HTML5 자바 스크립트 엔진입니다. 아마 내부에 clay.io를 구현할 것입니다.Socket.io Construct2의 여러 방

내 질문은 "방"에 관한 것입니다. Clay.io도 방을 도와줍니다. 그러나 나는 그 제안을 할 것인지 확신 할 수 없다. https://clay.io/docs/rooms

그래서 게임 당 사용자 수를 10 명으로 제한하고 싶습니다. 그러면 두 대의 서버를 실행해야합니까?

socket.io 서버는 recive하고 데이터를 반환합니다. 그러나 각 10 명이 실행중인 두 게임이 서버 데이터를 혼동하지 않을까요? 서버 A의 사람 A가 일부 사람 1을 쏘면이 정보가 서버 B의 사람 B에게 어떻게 든 끝날 수 있습니까? 또는 할당 된 ID로이 문제를 방지 할 수 있습니까? 여기

는 내 요구에 업그레이드 할 예 서버입니다 :

var entities = [], count = 0; 
    var io = require("socket.io").listen(8099); 

    var INITIAL_X = 5; 
    var INITIAL_Y = 5; 
    var INITIAL_VEL_X = 0; 
    var INITIAL_VEL_Y = 0; 

    io.set('log level', 1); 
    io.sockets.on("connection", function (socket) { 
     var myNumber = count++; 
     //assign number  
     var mySelf = entities[myNumber] = [myNumber, INITIAL_X, INITIAL_Y, INITIAL_VEL_X, INITIAL_VEL_Y]; 

     //Send the initial position and ID to connecting player 
    console.log(myNumber + ' sent: ' + 'I,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]); 
     socket.send('I,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]); 
     //Send to conencting client the current state of all the other players 
     for (var entity_idx = 0; entity_idx < entities.length; entity_idx++) { 
//send initial update 
      if (entity_idx != myNumber) { 
       entity = entities[entity_idx]; 
       if (typeof (entity) != "undefined" && entity != null) { 

       console.log(myNumber + ' sent: C for ' + entity_idx); 
       socket.send('C,' + entity[0] + ',' + entity[1] + ',' + entity[2]); 
//send the client that just connected the position of all the other clients 
      } 
     } 
    } 
    //create new entity in all clients  
    socket.broadcast.emit("message", 
     'C,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2]); 
    socket.on("message", function (data) { 

     //if (myNumber == 0) 
     // console.log(myNumber + ' sent: ' +data); 
     var new_data = data.split(','); 
     if (new_data[0] == 'UM') { 
      mySelf[1] = new_data[1]; 
      mySelf[2] = new_data[2]; 
      mySelf[3] = new_data[3]; 
      mySelf[4] = new_data[4]; 
      //Update all the other clients about my update 
      socket.broadcast.emit("message", 
      'UM,' + mySelf[0] + ',' + mySelf[1] + ',' + mySelf[2] + ',' + mySelf[3] + ',' + mySelf[4]); 
     } 
     else if (new_data[0] == 'S') { // a s message 
      var shoot_info = []; 
      shoot_info[0] = new_data[1]; //ini x 
      shoot_info[1] = new_data[2]; //ini y 

      shoot_info[2] = new_data[3]; //degrees 

      //Update all the other clients about my update 
      socket.broadcast.emit("message", 
      'S,' + mySelf[0] + ',' + shoot_info[0] + ',' + shoot_info[1] + ',' + shoot_info[2]); 
     } 
    }); 

}); 

답변

1

Socket.io는 참조로 방송을 제한 할 수 있습니다 객실이 있습니다 https://github.com/LearnBoost/socket.io/wiki/Rooms

을 다음보다는 socket.broadcast.emit를 사용을 io.sockets.in('roomname').emit

이것을 Clay.io에 메쉬로 묶는 좋은 방법은 room.id (roomId 표현식 인 Construct 2 플러그인에 있음)이라는 방 이름을 사용하는 것입니다. Clay.io 룸이 가득 차면 (C2에 해당 조건이 있음) 해당 고유 ID를 사용하여 Socket.io 룸을 만들고 "Rooms Filled"라는 방을 방금 호출 한 플레이어를 배치하십시오.

작곡 2 대신 CoffeeScript로 쓰여진 게임이기 때문에 약간 다르다는 것을 알고 있습니다 만, 우리는 슬라임 발리 게임에서 Clay.io rooms + Socket.io 룸을 사용하고 있습니다. Here is the code.