Commit fc2d177d authored by 18신대성's avatar 18신대성 Committed by 16이지혜

이제 클라이언트 접속시 고유 번호(id)를 할당받음

parent 303a919d
......@@ -17,6 +17,7 @@ var config = {
};
var game = new Phaser.Game(config)
var playerNum = -1;
// load assets
function preload()
......@@ -45,8 +46,11 @@ function update()
});
}
// client side
var socket = io.connect();
socket.on('hi', function(msg) {
console.log(msg);
socket.on('idSet', function(msg) // {str, num}
{
console.log(msg.str);
this.playerNum = msg.num;
});
socket.emit('hello');
\ No newline at end of file
socket.emit('idRequest');
\ No newline at end of file
......@@ -16,10 +16,35 @@ server.listen(80, function() {
console.log('Listening on port ' + server.address().port);
});
var GameServer = GameServer || {};
GameServer.waitingRoom = [];
GameServer.getPlayerNumber = function()
{
do
{
var num = Math.floor(Math.random() * 1000 + 1);
if (!this.waitingRoom.includes(num)) return num;
} while (true)
}
// 클라이언트 요청에 대한 콜백 정의
io.on('connection', function(socket) {
socket.on('hello', function() {
console.log('client request');
socket.emit('hi', 'Hello, Client!');
io.on('connection', function(socket)
{
socket.on('idRequest', function() {
var playerSocket =
{
id: GameServer.getPlayerNumber(),
socketId: socket
}
GameServer.waitingRoom.push(playerSocket);
console.log('client request, id: ' + playerSocket.id);
socket.emit('idSet',
{
str: 'your number is ' + playerSocket.id,
num: playerSocket.id
});
});
});
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment