Skip to content

Commit

Permalink
Add websocket proxy to allow Watching without HTTP partial reads
Browse files Browse the repository at this point in the history
This allows clients such as Unreal that support websockets but not HTTP
partial reads, to use streams such as the GameServer watch endpoint.
  • Loading branch information
highlyunavailable committed Feb 23, 2021
1 parent da4d592 commit 606eb17
Show file tree
Hide file tree
Showing 32 changed files with 3,899 additions and 79 deletions.
4 changes: 3 additions & 1 deletion build/build-sdk-images/restapi/sdktest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ cd /go/src/agones.dev/agones/test/sdk/restapi
# swagger gen has a bug wherein it doesn't generate the file, so we're providing it by hand
cp ./model_xstreamdefinitionssdkgameserver.go.nolint ./swagger/model_xstreamdefinitionssdkgameserver.go
cp ./http-api-test.go.nolint ./http-api-test.go
go run http-api-test.go
go run http-api-test.go
cd /go/src/agones.dev/agones/test/sdk/websocket-watch
go run ws-watch-test.go
3 changes: 2 additions & 1 deletion cmd/sdk-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/tmc/grpc-websocket-proxy/wsproxy"
"golang.org/x/net/context"
"google.golang.org/grpc"
"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -87,7 +88,7 @@ func main() {
mux := gwruntime.NewServeMux()
httpServer := &http.Server{
Addr: fmt.Sprintf("%s:%d", ctlConf.Address, ctlConf.HTTPPort),
Handler: mux,
Handler: wsproxy.WebsocketProxy(mux),
}
defer httpServer.Close() // nolint: errcheck
ctx, cancel := context.WithCancel(context.Background())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.3.2
github.com/stretchr/testify v1.5.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8
go.opencensus.io v0.22.3
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4
Expand Down
79 changes: 2 additions & 77 deletions go.sum

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions site/content/en/docs/Guides/Client SDKs/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ Response:
{"result":{"object_meta":{"name":"local","namespace":"default","uid":"1234","resource_version":"v1","generation":"1","creation_timestamp":"1533766607","annotations":{"annotation":"true"},"labels":{"islocal":"true"}},"status":{"state":"Ready","address":"127.0.0.1","ports":[{"name":"default","port":7777}]}}}
{"result":{"object_meta":{"name":"local","namespace":"default","uid":"1234","resource_version":"v1","generation":"1","creation_timestamp":"1533766607","annotations":{"annotation":"true"},"labels":{"islocal":"true"}},"status":{"state":"Ready","address":"127.0.0.1","ports":[{"name":"default","port":7777}]}}}
```

{{% feature publishVersion="1.3.0" %}}
The Watch GameServer stream is also exposed as a WebSocket endpoint on the same URL and port as the HTTP `watch/gameserver` API. This endpoint is provided as a convienence for streaming data to clients such as Unreal that support WebSocket but not HTTP streaming, and HTTP streaming should be used instead if possible.

An example command that uses the WebSocket endpoint instead of streaming over HTTP is:


```bash
curl -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Key: ExampleKey1234567890===" -H "Sec-WebSocket-Version: 13" -X GET http://localhost:${AGONES_SDK_HTTP_PORT}/watch/gameserver
```

The data returned from this endpoint is newline-delimited JSON objects and is identical to the response of the HTTP streaming watch endpoint shown above. When reading from the websocket endpoint, make sure to wait for a delimiter before trying to deserialize the JSON, as client buffers may be smaller than the delimited messages.
{{% /feature %}}

### Metadata Management

#### Set Label
Expand Down
102 changes: 102 additions & 0 deletions test/sdk/websocket-watch/ws-watch-test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2021 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"log"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/gorilla/websocket"
)

func main() {
portStr := os.Getenv("AGONES_SDK_HTTP_PORT")
watchURL := url.URL{Scheme: "ws", Host: "localhost:" + portStr, Path: "/watch/gameserver"}
readyURL := url.URL{Scheme: "http", Host: "localhost:" + portStr, Path: "/metadata/label"}
log.Printf("Connecting to %s", watchURL.String())
websocketClient, connectResponse, dialErr := websocket.DefaultDialer.Dial(watchURL.String(), nil)
httpClient := &http.Client{
Timeout: time.Second * 10,
}

if dialErr != nil {
log.Fatal("Could not dial watch websocket:", dialErr)
}

defer connectResponse.Body.Close() // nolint: errcheck

defer websocketClient.Close() // nolint: errcheck

done := make(chan struct{})

go func() {
defer close(done) // nolint: errcheck
_, message, err := websocketClient.ReadMessage()
if err != nil {
log.Fatalf("Unable to read message from websocket: %s", err)
return
}
log.Printf("Received message from websocket: %s", message)

if strings.Contains(string(message), "agones.dev/sdk-testws") {
log.Printf("Found label 'agones.dev/sdk-testws' in message")
} else {
log.Fatalf("Could not find label 'agones.dev/sdk-testws' in message")
}
done <- struct{}{}
}()

timeout := time.NewTicker(time.Second)
defer timeout.Stop()

tries := 0

req, reqErr := http.NewRequest("PUT", readyURL.String(), strings.NewReader("{\"key\": \"testws\", \"value\": \"true\"}"))

if reqErr != nil {
log.Fatalf("Could not create label request: %s", reqErr) // nolint: gocritic
}

response, respErr := httpClient.Do(req)

if respErr != nil {
log.Fatalf("Could not put label request: %s", reqErr) // nolint: gocritic
}

defer response.Body.Close() // nolint: errcheck

L:
for {
select {
case <-done:
break L
case <-timeout.C:
if tries > 10 {
log.Fatal("Test timed out")
}
tries++
}
}

closeErr := websocketClient.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))

if closeErr != nil {
log.Fatalf("Error writing close message: %s", closeErr)
}
}
25 changes: 25 additions & 0 deletions vendor/github.com/gorilla/websocket/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions vendor/github.com/gorilla/websocket/AUTHORS

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/github.com/gorilla/websocket/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions vendor/github.com/gorilla/websocket/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 606eb17

Please sign in to comment.