From 6bf86153260ddfd8ef1929323e0ee37cc3acd44a Mon Sep 17 00:00:00 2001 From: Ola <1386739+olahol@users.noreply.github.com> Date: Thu, 25 Jul 2024 20:07:11 +0200 Subject: [PATCH] Update gophers example --- README.md | 50 ++++++++-------------------------------- examples/go.mod | 1 - examples/go.sum | 3 +-- examples/gophers/main.go | 50 ++++++++-------------------------------- 4 files changed, 21 insertions(+), 83 deletions(-) diff --git a/README.md b/README.md index 53196b4..b6bdc89 100644 --- a/README.md +++ b/README.md @@ -63,16 +63,14 @@ func main() { package main import ( + "fmt" "net/http" - "strings" + "sync/atomic" - "github.com/google/uuid" "github.com/olahol/melody" ) -type GopherInfo struct { - ID, X, Y string -} +var idCounter atomic.Int64 func main() { m := melody.New() @@ -86,51 +84,23 @@ func main() { }) m.HandleConnect(func(s *melody.Session) { - ss, _ := m.Sessions() - - for _, o := range ss { - value, exists := o.Get("info") - - if !exists { - continue - } - - info := value.(*GopherInfo) - - s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y)) - } + id := idCounter.Add(1) - id := uuid.NewString() - s.Set("info", &GopherInfo{id, "0", "0"}) + s.Set("id", id) - s.Write([]byte("iam " + id)) + s.Write([]byte(fmt.Sprintf("iam %d", id))) }) m.HandleDisconnect(func(s *melody.Session) { - value, exists := s.Get("info") - - if !exists { - return + if id, ok := s.Get("id"); ok { + m.BroadcastOthers([]byte(fmt.Sprintf("dis %d", id)), s) } - - info := value.(*GopherInfo) - - m.BroadcastOthers([]byte("dis "+info.ID), s) }) m.HandleMessage(func(s *melody.Session, msg []byte) { - p := strings.Split(string(msg), " ") - value, exists := s.Get("info") - - if len(p) != 2 || !exists { - return + if id, ok := s.Get("id"); ok { + m.BroadcastOthers([]byte(fmt.Sprintf("set %d %s", id, msg)), s) } - - info := value.(*GopherInfo) - info.X = p[0] - info.Y = p[1] - - m.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s) }) http.ListenAndServe(":5000", nil) diff --git a/examples/go.mod b/examples/go.mod index 63fba14..2bd4800 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -7,7 +7,6 @@ replace github.com/olahol/melody => ../ require ( github.com/fsnotify/fsnotify v1.5.4 github.com/gin-gonic/gin v1.9.1 - github.com/google/uuid v1.3.0 github.com/labstack/echo/v4 v4.9.0 github.com/olahol/melody v0.0.0-00010101000000-000000000000 ) diff --git a/examples/go.sum b/examples/go.sum index afbb8ab..321346a 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -16,6 +16,7 @@ github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg= github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= @@ -30,8 +31,6 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= diff --git a/examples/gophers/main.go b/examples/gophers/main.go index 7de3c83..027b4ba 100644 --- a/examples/gophers/main.go +++ b/examples/gophers/main.go @@ -1,16 +1,14 @@ package main import ( + "fmt" "net/http" - "strings" + "sync/atomic" - "github.com/google/uuid" "github.com/olahol/melody" ) -type GopherInfo struct { - ID, X, Y string -} +var idCounter atomic.Int64 func main() { m := melody.New() @@ -24,51 +22,23 @@ func main() { }) m.HandleConnect(func(s *melody.Session) { - ss, _ := m.Sessions() - - for _, o := range ss { - value, exists := o.Get("info") - - if !exists { - continue - } - - info := value.(*GopherInfo) - - s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y)) - } + id := idCounter.Add(1) - id := uuid.NewString() - s.Set("info", &GopherInfo{id, "0", "0"}) + s.Set("id", id) - s.Write([]byte("iam " + id)) + s.Write([]byte(fmt.Sprintf("iam %d", id))) }) m.HandleDisconnect(func(s *melody.Session) { - value, exists := s.Get("info") - - if !exists { - return + if id, ok := s.Get("id"); ok { + m.BroadcastOthers([]byte(fmt.Sprintf("dis %d", id)), s) } - - info := value.(*GopherInfo) - - m.BroadcastOthers([]byte("dis "+info.ID), s) }) m.HandleMessage(func(s *melody.Session, msg []byte) { - p := strings.Split(string(msg), " ") - value, exists := s.Get("info") - - if len(p) != 2 || !exists { - return + if id, ok := s.Get("id"); ok { + m.BroadcastOthers([]byte(fmt.Sprintf("set %d %s", id, msg)), s) } - - info := value.(*GopherInfo) - info.X = p[0] - info.Y = p[1] - - m.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s) }) http.ListenAndServe(":5000", nil)