MCPcopy
hub / github.com/olahol/melody / main

Function main

examples/gophers/main.go:13–45  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11var idCounter atomic.Int64
12
13func main() {
14 m := melody.New()
15
16 http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
17 http.ServeFile(w, r, "index.html")
18 })
19
20 http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
21 m.HandleRequest(w, r)
22 })
23
24 m.HandleConnect(func(s *melody.Session) {
25 id := idCounter.Add(1)
26
27 s.Set("id", id)
28
29 s.Write([]byte(fmt.Sprintf("iam %d", id)))
30 })
31
32 m.HandleDisconnect(func(s *melody.Session) {
33 if id, ok := s.Get("id"); ok {
34 m.BroadcastOthers([]byte(fmt.Sprintf("dis %d", id)), s)
35 }
36 })
37
38 m.HandleMessage(func(s *melody.Session, msg []byte) {
39 if id, ok := s.Get("id"); ok {
40 m.BroadcastOthers([]byte(fmt.Sprintf("set %d %s", id, msg)), s)
41 }
42 })
43
44 http.ListenAndServe(":5000", nil)
45}

Callers

nothing calls this directly

Calls 9

NewFunction · 0.92
HandleRequestMethod · 0.80
HandleConnectMethod · 0.80
SetMethod · 0.80
WriteMethod · 0.80
HandleDisconnectMethod · 0.80
GetMethod · 0.80
BroadcastOthersMethod · 0.80
HandleMessageMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…