Skip to content

Commit 19fd3ce

Browse files
committed
Add test websocket connection
1 parent 64c580d commit 19fd3ce

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

ebiten-game/game/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
_ "image/jpeg"
2020
_ "image/png"
2121

22+
"github.com/coder/websocket"
23+
"github.com/coder/websocket/wsjson"
2224
"github.com/ebitengine/debugui"
2325
"github.com/hajimehoshi/ebiten/v2"
2426
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@@ -299,7 +301,26 @@ func (g *game) onHostReceivedDataChannel(d *webrtc.DataChannel) {
299301
})
300302
}
301303

304+
func (g *game) openWebsocket() {
305+
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
306+
defer cancel()
307+
308+
c, _, err := websocket.Dial(ctx, fmt.Sprintf("ws://%s:%d/lobbies", g.signalingIP, g.port), nil)
309+
if err != nil {
310+
println("Failed to connect to websocket:", err.Error())
311+
}
312+
defer c.CloseNow()
313+
314+
err = wsjson.Write(ctx, c, "open websocket")
315+
if err != nil {
316+
println("Failed to write websocket message:", err.Error())
317+
}
318+
319+
c.Close(websocket.StatusNormalClosure, "")
320+
}
321+
302322
func (g *game) startHost() {
323+
g.openWebsocket()
303324
g.writeLog("Hosting a lobby")
304325
// Host creates lobby.
305326
req, err := http.NewRequestWithContext(context.Background(), "GET", g.getSignalingURL()+"/lobby/host", nil)

ebiten-game/signaling-server/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,20 @@
44
package main
55

66
import (
7+
"context"
78
"encoding/json"
89
"errors"
910
"fmt"
1011
"io"
12+
"log"
1113
"math/rand"
1214
"net/http"
1315
"strconv"
1416
"sync"
17+
"time"
1518

19+
"github.com/coder/websocket"
20+
"github.com/coder/websocket/wsjson"
1621
"github.com/pion/webrtc/v4"
1722
"github.com/rs/cors"
1823
)
@@ -112,6 +117,7 @@ func main() {
112117
fmt.Printf("Failed to write response: %s", err)
113118
}
114119
})
120+
mux.HandleFunc("/lobbies", db.lobbies)
115121

116122
fmt.Println("Server started on port 3000")
117123
// cors.Default() setup the middleware with default options being
@@ -126,6 +132,29 @@ func main() {
126132
}
127133
}
128134

135+
func (db *lobbyDatabase) lobbies(w http.ResponseWriter, r *http.Request) {
136+
c, err := websocket.Accept(w, r, nil)
137+
if err != nil {
138+
println("Failed to accept websocket:", err.Error())
139+
}
140+
defer c.CloseNow()
141+
142+
// Set the context as needed. Use of r.Context() is not recommended
143+
// to avoid surprising behavior (see http.Hijacker).
144+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
145+
defer cancel()
146+
147+
var v any
148+
err = wsjson.Read(ctx, c, &v)
149+
if err != nil {
150+
println("Failed to read websocket message:", err.Error())
151+
}
152+
153+
log.Printf("received: %v", v)
154+
155+
c.Close(websocket.StatusNormalClosure, "")
156+
}
157+
129158
func (db *lobbyDatabase) lobbyHost(res http.ResponseWriter, _ *http.Request) {
130159
lobbyID := db.makeLobby()
131160
lobby := db.lobbyList[lobbyID]

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ toolchain go1.25.1
77
require (
88
github.com/asticode/go-astiav v0.19.0
99
github.com/at-wat/ebml-go v0.17.2
10+
github.com/coder/websocket v1.8.14
1011
github.com/ebitengine/debugui v0.1.1
1112
github.com/emiago/sipgo v0.33.0
1213
github.com/go-gst/go-gst v1.3.0
@@ -29,7 +30,6 @@ require (
2930

3031
require (
3132
github.com/asticode/go-astikit v0.42.0 // indirect
32-
github.com/coder/websocket v1.8.14 // indirect
3333
github.com/ebitengine/gomobile v0.0.0-20240911145611-4856209ac325 // indirect
3434
github.com/ebitengine/hideconsole v1.0.0 // indirect
3535
github.com/ebitengine/purego v0.8.0 // indirect

0 commit comments

Comments
 (0)