mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 17:47:11 +00:00
Add overlord
This commit is contained in:
parent
df8c478580
commit
ed501f93e6
2 changed files with 125 additions and 8 deletions
19
main.go
19
main.go
|
|
@ -95,7 +95,14 @@ func main() {
|
|||
http.HandleFunc("/ws", ws)
|
||||
|
||||
if !IsOverlord {
|
||||
oclient = NewOverlordClient()
|
||||
conn, err := createOverlordConnection()
|
||||
if err != nil {
|
||||
log.Println("Cannot connect to overlord")
|
||||
log.Println("Run as a single server")
|
||||
oclient = nil
|
||||
} else {
|
||||
oclient = NewOverlordClient(conn)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("oclient ", oclient)
|
||||
|
|
@ -470,13 +477,7 @@ func createOverlordConnection() (*websocket.Conn, error) {
|
|||
return c, nil
|
||||
}
|
||||
|
||||
func NewOverlordClient() *Client {
|
||||
oc, err := createOverlordConnection()
|
||||
if err != nil {
|
||||
log.Println("Cannot connect to overlord")
|
||||
log.Println("Run as a single server")
|
||||
return nil
|
||||
}
|
||||
func NewOverlordClient(oc *websocket.Conn) *Client {
|
||||
oclient := NewClient(oc)
|
||||
|
||||
// Received from overlord the serverID
|
||||
|
|
@ -523,7 +524,9 @@ func NewOverlordClient() *Client {
|
|||
log.Println("Add the connection to current room on the host")
|
||||
|
||||
peerconnection := peerconnections[resp.SessionID]
|
||||
log.Println("start session")
|
||||
roomID, isNewRoom := startSession(peerconnection, resp.Data, resp.RoomID, resp.PlayerIndex)
|
||||
log.Println("Done, sending back")
|
||||
// Bridge always access to old room
|
||||
// TODO: log warn
|
||||
if isNewRoom == true {
|
||||
|
|
|
|||
114
main_test.go
Normal file
114
main_test.go
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
gamertc "github.com/giongto35/cloud-game/webrtc"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/pion/webrtc"
|
||||
)
|
||||
|
||||
var host = "http://localhost:8000"
|
||||
var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}}
|
||||
|
||||
func initOverlord() *httptest.Server {
|
||||
overlord := httptest.NewServer(http.HandlerFunc(wso))
|
||||
return overlord
|
||||
}
|
||||
|
||||
func initServer(t *testing.T, overlordURL string) *httptest.Server {
|
||||
u := "ws" + strings.TrimPrefix(overlordURL, "http")
|
||||
fmt.Println("connecting to overlord: ", u)
|
||||
|
||||
oconn, _, err := websocket.DefaultDialer.Dial(u, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
oclient = NewOverlordClient(oconn)
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(ws))
|
||||
return server
|
||||
}
|
||||
|
||||
func initClient(t *testing.T, host string) {
|
||||
// Convert http://127.0.0.1 to ws://127.0.0.
|
||||
u := "ws" + strings.TrimPrefix(host, "http")
|
||||
|
||||
// Connect to the server
|
||||
ws, _, err := websocket.DefaultDialer.Dial(u, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
defer ws.Close()
|
||||
|
||||
// Simulate peerconnection initialization from client
|
||||
|
||||
peerConnection, err := webrtc.NewPeerConnection(webrtcconfig)
|
||||
if err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
offer, err := peerConnection.CreateOffer(nil)
|
||||
if err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
// Sets the LocalDescription, and starts our UDP listeners
|
||||
err = peerConnection.SetLocalDescription(offer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Send offer to server
|
||||
client := NewClient(ws)
|
||||
go client.listen()
|
||||
|
||||
fmt.Println("Sending offer...")
|
||||
client.send(WSPacket{
|
||||
ID: "initwebrtc",
|
||||
Data: gamertc.Encode(offer),
|
||||
}, nil)
|
||||
fmt.Println("Waiting sdp...")
|
||||
|
||||
client.receive("sdp", func(resp WSPacket) WSPacket {
|
||||
fmt.Println("received", resp.Data)
|
||||
answer := webrtc.SessionDescription{}
|
||||
gamertc.Decode(resp.Data, &answer)
|
||||
// Apply the answer as the remote description
|
||||
err = peerConnection.SetRemoteDescription(answer)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return EmptyPacket
|
||||
})
|
||||
|
||||
time.Sleep(time.Second * 3)
|
||||
fmt.Println("Sending start...")
|
||||
client.send(WSPacket{
|
||||
ID: "start",
|
||||
Data: "Contra.nes",
|
||||
RoomID: "",
|
||||
PlayerIndex: 1,
|
||||
}, func(resp WSPacket) {
|
||||
fmt.Println("Received response")
|
||||
fmt.Println("RoomID:", resp.RoomID)
|
||||
})
|
||||
|
||||
time.Sleep(time.Hour)
|
||||
}
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
o := initOverlord()
|
||||
defer o.Close()
|
||||
// Init slave server
|
||||
s := initServer(t, o.URL)
|
||||
defer s.Close()
|
||||
|
||||
initClient(t, s.URL)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue