Skip to content

Commit a4e1975

Browse files
authored
handler: Stop using pubsub package (#113)
* handler: Stop using pubsub package * chore: Delete unused file
1 parent b73d331 commit a4e1975

13 files changed

Lines changed: 28 additions & 159 deletions

File tree

server/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.26.0
55
tool github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen
66

77
require (
8-
github.com/WillYingling/pubsub v0.0.0-20231109151540-60af269b467d
98
github.com/alecthomas/kong v1.15.0
109
github.com/go-sql-driver/mysql v1.10.0
1110
github.com/google/uuid v1.6.0

server/go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4
22
filippo.io/edwards25519 v1.2.0 h1:crnVqOiS4jqYleHd9vaKZ+HKtHfllngJIiOpNpoJsjo=
33
filippo.io/edwards25519 v1.2.0/go.mod h1:xzAOLCNug/yB62zG1bQ8uziwrIqIuxhctzJT18Q77mc=
44
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
5-
github.com/WillYingling/pubsub v0.0.0-20231109151540-60af269b467d h1:BHMCJSu0ASytkeqEcWgxzniyuOVCoTdepsNwresxWn0=
6-
github.com/WillYingling/pubsub v0.0.0-20231109151540-60af269b467d/go.mod h1:0Y+Zm3/cXFCywxBM7ThwQ2IFVxulqNH0w/hOP7Rw8XI=
75
github.com/alecthomas/assert/v2 v2.11.0 h1:2Q9r3ki8+JYXvGsDyBXwH3LcJ+WK5D0gc5E8vS6K3D0=
86
github.com/alecthomas/assert/v2 v2.11.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
97
github.com/alecthomas/kong v1.15.0 h1:BVJstKbpO73zKpmIu+m/aLRrNmWwxXPIGTNin9VmLVI=

server/handler/handler.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/traP-jp/h26s_01/server/handler/rest"
88
"github.com/traP-jp/h26s_01/server/handler/socketio"
99
"github.com/traP-jp/h26s_01/server/repository"
10+
"github.com/zishang520/socket.io/servers/socket/v3"
1011
)
1112

1213
type Server struct {
@@ -16,10 +17,12 @@ type Server struct {
1617
}
1718

1819
func NewServer(config *config.Config, repo *repository.Repository) *Server {
20+
ioServer := socket.NewServer(nil, nil)
21+
1922
return &Server{
2023
config: config,
21-
restAPIHandler: rest.NewHandler(repo),
22-
socketIOHandler: socketio.NewHandler(repo),
24+
restAPIHandler: rest.NewHandler(repo, ioServer),
25+
socketIOHandler: socketio.NewHandler(repo, ioServer),
2326
}
2427
}
2528

server/handler/rest/handler.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@ import (
44
"github.com/labstack/echo/v5"
55
"github.com/traP-jp/h26s_01/server/model"
66
"github.com/traP-jp/h26s_01/server/repository"
7+
"github.com/zishang520/socket.io/servers/socket/v3"
78
)
89

910
type Handler struct {
1011
repo *repository.Repository
12+
io *socket.Server
1113
}
1214

13-
func NewHandler(repo *repository.Repository) *Handler {
15+
func NewHandler(repo *repository.Repository, io *socket.Server) *Handler {
1416
return &Handler{
1517
repo: repo,
18+
io: io,
1619
}
1720
}
1821

server/handler/rest/room.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package rest
33
import (
44
"net/http"
55

6-
"github.com/WillYingling/pubsub"
76
"github.com/labstack/echo/v5"
87
openapi_types "github.com/oapi-codegen/runtime/types"
98
"github.com/traP-jp/h26s_01/server/api"
@@ -78,7 +77,7 @@ func (h *Handler) CreateRoom(c *echo.Context) error {
7877
return echo.ErrInternalServerError.Wrap(err)
7978
}
8079

81-
pubsub.Publish(c.Request().Context(), &roomListUpdatedEvent)
80+
h.io.Emit("room_list:updated", &roomListUpdatedEvent)
8281

8382
return c.JSON(http.StatusCreated, response)
8483
}

server/handler/socketio/answer.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55
"errors"
66
"time"
77

8-
"github.com/WillYingling/pubsub"
98
"github.com/google/uuid"
109
"github.com/traP-jp/h26s_01/server/api"
1110
"github.com/zishang520/socket.io/servers/socket/v3"
1211
)
1312

14-
func (h *Handler) handleAnswerSubmit(socket *socket.Socket, event api.AnswerSubmitEvent) error {
15-
rooms := socket.Rooms()
16-
myID := string(socket.Id())
13+
func (h *Handler) handleAnswerSubmit(s *socket.Socket, event api.AnswerSubmitEvent) error {
14+
rooms := s.Rooms()
15+
myID := string(s.Id())
1716
var err error
1817
var roomID uuid.UUID
1918

@@ -56,7 +55,7 @@ func (h *Handler) handleAnswerSubmit(socket *socket.Socket, event api.AnswerSubm
5655
GuesserAnswer: event.Answer,
5756
}
5857

59-
pubsub.Publish(context.Background(), roundAnswerEvent)
58+
h.io.To(socket.Room(roomID.String())).Emit("round:answer", roundAnswerEvent)
6059

6160
return nil
6261
}

server/handler/socketio/draw.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"log/slog"
66

7-
"github.com/WillYingling/pubsub"
87
"github.com/google/uuid"
98
"github.com/traP-jp/h26s_01/server/api"
109
"github.com/traP-jp/h26s_01/server/model"
@@ -149,7 +148,7 @@ func (h *Handler) handleDrawStroke(s *socket.Socket, event api.DrawStrokeEvent)
149148
TurnIndex: int(nextTurn.TurnIndex),
150149
}
151150
slog.Info("[draw:stroke] publishing turn:started event", "event", turnStartedEvent)
152-
pubsub.Publish(context.Background(), turnStartedEvent)
151+
h.io.To(roomID).Emit("turn:started", turnStartedEvent)
153152
slog.Info("[draw:stroke] turn:started event published")
154153

155154
return nil

server/handler/socketio/game.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"log/slog"
77

8-
"github.com/WillYingling/pubsub"
98
"github.com/google/uuid"
109
"github.com/traP-jp/h26s_01/server/api"
1110
"github.com/zishang520/socket.io/servers/socket/v3"
@@ -67,8 +66,8 @@ func (h *Handler) handleGameReady(s *socket.Socket) error {
6766
return err
6867
}
6968

70-
pubsub.Publish(context.Background(), &roomListUpdatedEvent)
71-
pubsub.Publish(context.Background(), &roomUpdatedEvent)
69+
h.io.Emit("room_list:updated", &roomListUpdatedEvent)
70+
h.io.To(socket.Room(roomID.String())).Emit("room:updated", &roomUpdatedEvent)
7271

7372
slog.Info("Checking if all users are ready", "roomID", roomID)
7473

@@ -95,8 +94,8 @@ func (h *Handler) handleGameReady(s *socket.Socket) error {
9594
return err
9695
}
9796

98-
pubsub.Publish(context.Background(), &roomListUpdatedEvent)
99-
pubsub.Publish(context.Background(), &roomUpdatedEvent)
97+
h.io.Emit("room_list:updated", &roomListUpdatedEvent)
98+
h.io.To(socket.Room(roomID.String())).Emit("room:updated", &roomUpdatedEvent)
10099

101100
slog.Info("Game started, beginning first round", "roomID", roomID)
102101
h.handleRoundStarted(s, roomID)

server/handler/socketio/handler.go

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ type Handler struct {
1818
repo *repository.Repository
1919
}
2020

21-
func NewHandler(repo *repository.Repository) *Handler {
22-
ioServer := socket.NewServer(nil, nil)
23-
21+
func NewHandler(repo *repository.Repository, ioServer *socket.Server) *Handler {
2422
return &Handler{
2523
ServeHandler: ioServer.ServeHandler(nil),
2624
io: ioServer,
@@ -119,28 +117,8 @@ func (h *Handler) registerEventHandlers(socket *socket.Socket) {
119117
socket.On("client:disconnect", createEventListenerForHandlersWithoutBody(socket, h.handleClientDisconnect))
120118

121119
go func() {
122-
if err := h.handleRoomListUpdated(socket); err != nil {
123-
slog.Error("handling room list updated", "error", err)
124-
}
125-
}()
126-
go func() {
127-
if err := h.roomUpdatedEventHandler(socket); err != nil {
128-
slog.Error("handling room updated", "error", err)
129-
}
130-
}()
131-
go func() {
132-
if err := h.handleRoundAnswer(socket); err != nil {
133-
slog.Error("handling round answer", "error", err)
134-
}
135-
}()
136-
go func() {
137-
if err := h.handleRoundStartedEvent(socket); err != nil {
138-
slog.Error("handling round started", "error", err)
139-
}
140-
}()
141-
go func() {
142-
if err := h.handleTurnStartedEvent(socket); err != nil {
143-
slog.Error("handling turn started", "error", err)
144-
}
120+
socket.On("disconnect", func(args ...any) {
121+
slog.Info("Client disconnected", "socketID", socket.Id())
122+
})
145123
}()
146124
}

server/handler/socketio/room.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66

7-
"github.com/WillYingling/pubsub"
87
"github.com/traP-jp/h26s_01/server/api"
98
"github.com/zishang520/socket.io/servers/socket/v3"
109
)
@@ -46,26 +45,8 @@ func (h *Handler) handleJoinRoom(s *socket.Socket, event api.RoomJoinEvent) erro
4645
return err
4746
}
4847

49-
pubsub.Publish(context.Background(), &roomListUpdatedEvent)
50-
pubsub.Publish(context.Background(), &roomUpdatedEvent)
48+
h.io.Emit("room_list:updated", &roomListUpdatedEvent)
49+
h.io.To(socket.Room(event.RoomId.String())).Emit("room:updated", &roomUpdatedEvent)
5150

5251
return nil
5352
}
54-
55-
func (h *Handler) roomUpdatedEventHandler(s *socket.Socket) error {
56-
ctx := context.Background()
57-
eventCh, unsubscribe := pubsub.SubscribeTo[*api.RoomUpdatedEvent](ctx)
58-
59-
s.On("disconnect", func(args ...any) {
60-
unsubscribe()
61-
})
62-
63-
for {
64-
select {
65-
case <-ctx.Done():
66-
return nil
67-
case event := <-eventCh:
68-
s.Emit("room:updated", event)
69-
}
70-
}
71-
}

0 commit comments

Comments
 (0)