diff --git a/client/src/index.js b/client/src/index.js index 629c0ae..beb23e0 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -18,7 +18,7 @@ class Game extends Phaser.Scene { this.player = null; this.wasd = {}; - this.websocket = new Websocket(this.handleMessage); + this.websocket = new Websocket(this.handleMessage.bind(this)); } preload() { @@ -47,28 +47,51 @@ class Game extends Phaser.Scene { } handlePlayerMove() { - this.player.body.setVelocity(0); - + let moved = false; if (this.cursors.left.isDown || this.wasd.A.isDown) { this.player.body.setVelocityX(-300); + moved = true; } else if (this.cursors.right.isDown || this.wasd.D.isDown) { this.player.body.setVelocityX(300); + moved = true; + } else { + this.player.body.setVelocityX(0); } if (this.cursors.down.isDown || this.wasd.S.isDown) { this.player.body.setVelocityY(300); + moved = true; } else if (this.cursors.up.isDown || this.wasd.W.isDown) { this.player.body.setVelocityY(-300); + moved = true; + } else { + this.player.body.setVelocityY(0); + } + if (moved) { + this.websocket.playerMoveSend(this.player.x, this.player.y, this.player.body.velocity.x, this.player.body.velocity.y); } } handleMessage(msg) { + console.log('handle message executed'); if (msg.hasJoin()) { - // TODO: Logic + console.log('player joined'); + this.joinRecieve(msg.getPlayerId()); } else if (msg.hasMove()) { - // TODO: Logic + const move = msg.getMove(); + this.playerMoveRecieve(msg.getPlayerId(), move.getX(), move.getY(), move.getDx(), move.getDy()); } } + + playerMoveRecieve(player_id, x, y, dx, dy) { + console.log(player_id, x, y); + + } + + joinRecieve(player_id) { + console.log(player_id); + + } } const config = { diff --git a/client/src/network/websocket.js b/client/src/network/websocket.js index 659e134..b5f953d 100644 --- a/client/src/network/websocket.js +++ b/client/src/network/websocket.js @@ -16,13 +16,14 @@ export class Websocket { this.sendMessage(message); }); + + // Listen for messages this.socket.addEventListener('message', (event) => { if (this.receiveCallback == null || !event.data) { return; } - event.data.arrayBuffer().then(data => { var message = messages.Message.deserializeBinary(data); this.receiveCallback(message); @@ -35,4 +36,15 @@ export class Websocket { var bytes = message.serializeBinary(); this.socket.send(bytes); } + + playerMoveSend(x, y, dx, dy) { + var message = new messages.Message(); + var move = new messages.Move(); + message.setMove(move); + move.setX(x); + move.setY(y); + move.setDx(dx); + move.setDy(dy); + this.sendMessage(message); + } } diff --git a/server/internal/ws/client.go b/server/internal/ws/client.go index fd3f070..e51c783 100644 --- a/server/internal/ws/client.go +++ b/server/internal/ws/client.go @@ -81,7 +81,7 @@ func (c *clientImpl) ReadPump() { c.logger.Debugw("started read pump for client", "id", c.id, "remoteAddr", c.addr) - defer c.Close() // + //defer c.Close() // for { _, msg, err := c.conn.ReadMessage() if err != nil { @@ -103,6 +103,8 @@ func (c *clientImpl) ReadPump() { events.Publish("server.broadcast", c.id, message) } + + c.server.Disconnect(c) } // SendPump sends messages to client and checks if there is an error and returns it diff --git a/server/internal/ws/server.go b/server/internal/ws/server.go index dded0b3..3b16f4d 100644 --- a/server/internal/ws/server.go +++ b/server/internal/ws/server.go @@ -51,8 +51,9 @@ func NewServer(logger *zap.Logger) Server { clients: make(map[int32]Client), // Make buffered channels - connect: make(chan connectMessage, consts.ChanBufferSize), - broadcast: make(chan broadcastMessage, consts.ChanBufferSize), + connect: make(chan connectMessage, consts.ChanBufferSize), + broadcast: make(chan broadcastMessage, consts.ChanBufferSize), + disconnect: make(chan Client, consts.ChanBufferSize), } }