Skip to content
Draft
Show file tree
Hide file tree
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
25 changes: 25 additions & 0 deletions ebiten-game/game/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
_ "image/jpeg"
_ "image/png"

"github.com/coder/websocket"
"github.com/coder/websocket/wsjson"
"github.com/ebitengine/debugui"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
Expand Down Expand Up @@ -299,6 +301,29 @@
})
}

func (g *game) openWebsocket() {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

c, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://%s:%d/host", g.signalingIP, g.port), nil)

Check failure on line 308 in ebiten-game/game/main.go

View workflow job for this annotation

GitHub Actions / lint / Go

response body must be closed (bodyclose)
if err != nil {
println("Failed to connect to websocket:", err.Error())
}
defer c.CloseNow()

Check failure on line 312 in ebiten-game/game/main.go

View workflow job for this annotation

GitHub Actions / lint / Go

Error return value of `c.CloseNow` is not checked (errcheck)

ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()

for range ticker.C {
err = wsjson.Write(ctx, c, fmt.Sprintf("ping from client at %s", time.Now().String()))
if err != nil {
println("Failed to write websocket message:", err.Error())
}
}

c.Close(websocket.StatusNormalClosure, "")

Check failure on line 324 in ebiten-game/game/main.go

View workflow job for this annotation

GitHub Actions / lint / Go

Error return value of `c.Close` is not checked (errcheck)
}

func (g *game) startHost() {
g.writeLog("Hosting a lobby")
// Host creates lobby.
Expand Down
2 changes: 1 addition & 1 deletion ebiten-game/game/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (g *game) logWindow(ctx *debugui.Context) {
ctx.GridCell(func(bounds image.Rectangle) {
submitOpen := func() {
g.isHost = true
g.startConnection()
g.openWebsocket()
}

submitJoin := func() {
Expand Down
Loading
Loading