Created
May 4, 2020 22:49
-
-
Save Franckapik/e6f5c9448b79f8d9e13ff8b644a98961 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const server = require('http').Server(app); | |
const io = require('socket.io')(server); | |
io.set('origins', '*:*'); | |
server.listen(3002); | |
const players = []; | |
const socketIO = () => { | |
//CONNECTION | |
io.on("connection", (socket) => { | |
players[socket.id] = { | |
x: 300, | |
y: 300, | |
score : 10 | |
}; | |
//ENVOI ID | |
console.log("Identification du joueur", socket.id); | |
socket.emit('id', socket.id) | |
//RECEPTION KEYBOARD | |
socket.on('movement', function (data) { | |
var player = players[socket.id] || {}; | |
if (data.left) { | |
player.x -= 5; | |
} | |
if (data.up) { | |
player.y -= 5; | |
player.score +=10; | |
console.log(players); | |
} | |
if (data.right) { | |
player.x += 5; | |
} | |
if (data.down) { | |
player.y += 5; | |
} | |
console.log(players); | |
io.emit('stateok', players); | |
}); | |
//DECONNECTION | |
socket.on("disconnect", () => { | |
console.log("Joueur déconnecté", socket.id); | |
var i = players.indexOf(socket.id); | |
if (i !== -1) { | |
players.splice(i, 1); | |
} | |
console.log(players); | |
io.emit('disconnect', players); | |
}); | |
}); | |
} | |
module.exports = socketIO; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment