Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions src/routers/ws/playRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,31 @@ const getPlayRoutes = (): expressWs.Router => {
return;
}

const player:Player = {
const player: Player = {
id: playerId,
connection: ws
}
currentRoom.set(playerId,player);
currentRoom.set(playerId, player);

//

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove uneccessary comment mark

currentRoom.forEach((rplayer) => {

@GiveMeSomething GiveMeSomething Jul 3, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Đang không hiểu đoạn forEach có chức năng gì 😵‍💫

Nếu được thì ông thêm explanation vào đây và thêm comments nhé

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEach được chạy khi có player bất kì ngắt kết nối. Nhiệm vụ gửi thông báo cho các player còn lại trong room về việc người chơi này đã ngắt kết nối

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hình như nhầm đoạn forEach rồi thì phải. Tôi thấy đoạn này gửi message type là SUBSCRIBE

Cái t không hiểu lắm là tại sao tại phải có message type là SUBSCRIBE

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

currentRoom.forEach((rplayer) => {

if (rplayer.id === player.id) {
return;
}
player.connection.send(JSON.stringify(
{
method: 'SUBSCRIBE',
player: rplayer.id
}
));
rplayer.connection.send(JSON.stringify(
{
method: 'SUBSCRIBE',
player: player.id
}
));
});
//

ws.on('message', (data) => {
const message = JSON.parse(data.toString());
Expand All @@ -64,7 +84,12 @@ const getPlayRoutes = (): expressWs.Router => {
if (rplayer.id === player.id) {
return;
}
rplayer.connection.send(JSON.stringify({player: player.id, messsage: message}));
rplayer.connection.send(JSON.stringify(
{
method: 'MESSAGE',
player: player.id, message: message
}
));
});
});

Expand All @@ -74,6 +99,15 @@ const getPlayRoutes = (): expressWs.Router => {

if (currentRoom.size) {
roomList.set(roomId, currentRoom);

currentRoom.forEach((rplayer) => {
rplayer.connection.send(JSON.stringify(
{
method: 'UNSUBSCRIBE',
player: player.id
}
));
});
} else {
roomList.delete(roomId)
}
Expand Down