Update gophers example.

This commit is contained in:
Ola
2022-09-14 15:34:50 +02:00
parent fdfd3d60e4
commit f46f868ce3
5 changed files with 85 additions and 31 deletions
+2 -2
View File
@@ -22,7 +22,7 @@
<script>
var url = "ws://" + window.location.host + "/ws";
var ws = new WebSocket(url);
var myid = -1;
var myid = "";
ws.onmessage = function (msg) {
var cmds = {"iam": iam, "set": set, "dis": dis};
@@ -60,7 +60,7 @@
}
window.onmousemove = function (e) {
if (myid > -1) {
if (myid !== "") {
set(myid, e.pageX, e.pageY);
ws.send([e.pageX, e.pageY].join(" "));
}
+13 -19
View File
@@ -2,15 +2,13 @@ package main
import (
"net/http"
"strconv"
"strings"
"sync"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/olahol/melody"
)
// GopherInfo contains information about the gopher on screen
type GopherInfo struct {
ID, X, Y string
}
@@ -18,9 +16,6 @@ type GopherInfo struct {
func main() {
router := gin.Default()
mrouter := melody.New()
gophers := make(map[*melody.Session]*GopherInfo)
lock := new(sync.Mutex)
counter := 0
router.GET("/", func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "index.html")
@@ -31,33 +26,32 @@ func main() {
})
mrouter.HandleConnect(func(s *melody.Session) {
lock.Lock()
for _, info := range gophers {
ss, _ := mrouter.Sessions()
for _, o := range ss {
info := o.MustGet("info").(*GopherInfo)
s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
}
gophers[s] = &GopherInfo{strconv.Itoa(counter), "0", "0"}
s.Write([]byte("iam " + gophers[s].ID))
counter++
lock.Unlock()
id := uuid.NewString()
s.Set("info", &GopherInfo{id, "0", "0"})
s.Write([]byte("iam " + id))
})
mrouter.HandleDisconnect(func(s *melody.Session) {
lock.Lock()
mrouter.BroadcastOthers([]byte("dis "+gophers[s].ID), s)
delete(gophers, s)
lock.Unlock()
info := s.MustGet("info").(*GopherInfo)
mrouter.BroadcastOthers([]byte("dis "+info.ID), s)
})
mrouter.HandleMessage(func(s *melody.Session, msg []byte) {
p := strings.Split(string(msg), " ")
lock.Lock()
info := gophers[s]
if len(p) == 2 {
info := s.MustGet("info").(*GopherInfo)
info.X = p[0]
info.Y = p[1]
mrouter.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s)
}
lock.Unlock()
})
router.Run(":5000")