Skip to content

Commit

Permalink
Update gophers example
Browse files Browse the repository at this point in the history
  • Loading branch information
olahol committed Jul 25, 2024
1 parent ca3b7af commit 6bf8615
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 83 deletions.
50 changes: 10 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
3 changes: 1 addition & 2 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
50 changes: 10 additions & 40 deletions examples/gophers/main.go
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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)
Expand Down

0 comments on commit 6bf8615

Please sign in to comment.