Skip to content

Commit

Permalink
Merge pull request #37 from spelag/websocket
Browse files Browse the repository at this point in the history
 fix server crash disconnect + handleMessage in index
  • Loading branch information
davidmohar authored Aug 19, 2021
2 parents a12f37f + 37c8112 commit 39142a9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
33 changes: 28 additions & 5 deletions client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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 = {
Expand Down
14 changes: 13 additions & 1 deletion client/src/network/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
4 changes: 3 additions & 1 deletion server/internal/ws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions server/internal/ws/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}

Expand Down

0 comments on commit 39142a9

Please sign in to comment.