github.com/philippseith/signalr@v0.6.3/chatsample/main.go (about) 1 package main 2 3 import ( 4 "context" 5 _ "embed" 6 "fmt" 7 "log" 8 "net/http" 9 "os" 10 "strings" 11 "time" 12 13 kitlog "github.com/go-kit/log" 14 15 "github.com/philippseith/signalr" 16 "github.com/philippseith/signalr/chatsample/middleware" 17 "github.com/philippseith/signalr/chatsample/public" 18 ) 19 20 type chat struct { 21 signalr.Hub 22 } 23 24 func (c *chat) OnConnected(connectionID string) { 25 fmt.Printf("%s connected\n", connectionID) 26 c.Groups().AddToGroup("group", connectionID) 27 } 28 29 func (c *chat) OnDisconnected(connectionID string) { 30 fmt.Printf("%s disconnected\n", connectionID) 31 c.Groups().RemoveFromGroup("group", connectionID) 32 } 33 34 func (c *chat) Broadcast(message string) { 35 // Broadcast to all clients 36 c.Clients().Group("group").Send("receive", message) 37 } 38 39 func (c *chat) Echo(message string) { 40 c.Clients().Caller().Send("receive", message) 41 } 42 43 func (c *chat) Panic() { 44 panic("Don't panic!") 45 } 46 47 func (c *chat) RequestAsync(message string) <-chan map[string]string { 48 r := make(chan map[string]string) 49 go func() { 50 defer close(r) 51 time.Sleep(4 * time.Second) 52 m := make(map[string]string) 53 m["ToUpper"] = strings.ToUpper(message) 54 m["ToLower"] = strings.ToLower(message) 55 m["len"] = fmt.Sprint(len(message)) 56 r <- m 57 }() 58 return r 59 } 60 61 func (c *chat) RequestTuple(message string) (string, string, int) { 62 return strings.ToUpper(message), strings.ToLower(message), len(message) 63 } 64 65 func (c *chat) DateStream() <-chan string { 66 r := make(chan string) 67 go func() { 68 defer close(r) 69 for i := 0; i < 50; i++ { 70 r <- fmt.Sprint(time.Now().Clock()) 71 time.Sleep(time.Second) 72 } 73 }() 74 return r 75 } 76 77 func (c *chat) UploadStream(upload1 <-chan int, factor float64, upload2 <-chan float64) { 78 ok1 := true 79 ok2 := true 80 u1 := 0 81 u2 := 0.0 82 c.Echo(fmt.Sprintf("f: %v", factor)) 83 for { 84 select { 85 case u1, ok1 = <-upload1: 86 if ok1 { 87 c.Echo(fmt.Sprintf("u1: %v", u1)) 88 } else if !ok2 { 89 c.Echo("Finished") 90 return 91 } 92 case u2, ok2 = <-upload2: 93 if ok2 { 94 c.Echo(fmt.Sprintf("u2: %v", u2)) 95 } else if !ok1 { 96 c.Echo("Finished") 97 return 98 } 99 } 100 } 101 } 102 103 func (c *chat) Abort() { 104 fmt.Println("Abort") 105 c.Hub.Abort() 106 } 107 108 //func runTCPServer(address string, hub signalr.HubInterface) { 109 // listener, err := net.Listen("tcp", address) 110 // 111 // if err != nil { 112 // fmt.Println(err) 113 // return 114 // } 115 // 116 // fmt.Printf("Listening for TCP connection on %s\n", listener.Addr()) 117 // 118 // server, _ := signalr.NewServer(context.TODO(), signalr.UseHub(hub)) 119 // 120 // for { 121 // conn, err := listener.Accept() 122 // 123 // if err != nil { 124 // fmt.Println(err) 125 // break 126 // } 127 // 128 // go server.Serve(context.TODO(), newNetConnection(conn)) 129 // } 130 //} 131 132 func runHTTPServer(address string, hub signalr.HubInterface) { 133 server, _ := signalr.NewServer(context.TODO(), signalr.SimpleHubFactory(hub), 134 signalr.Logger(kitlog.NewLogfmtLogger(os.Stdout), false), 135 signalr.KeepAliveInterval(2*time.Second)) 136 router := http.NewServeMux() 137 server.MapHTTP(signalr.WithHTTPServeMux(router), "/chat") 138 139 fmt.Printf("Serving public content from the embedded filesystem\n") 140 router.Handle("/", http.FileServer(http.FS(public.FS))) 141 fmt.Printf("Listening for websocket connections on http://%s\n", address) 142 if err := http.ListenAndServe(address, middleware.LogRequests(router)); err != nil { 143 log.Fatal("ListenAndServe:", err) 144 } 145 } 146 147 func runHTTPClient(address string, receiver interface{}) error { 148 c, err := signalr.NewClient(context.Background(), nil, 149 signalr.WithReceiver(receiver), 150 signalr.WithConnector(func() (signalr.Connection, error) { 151 creationCtx, _ := context.WithTimeout(context.Background(), 2*time.Second) 152 return signalr.NewHTTPConnection(creationCtx, address) 153 }), 154 signalr.Logger(kitlog.NewLogfmtLogger(os.Stdout), false)) 155 if err != nil { 156 return err 157 } 158 c.Start() 159 fmt.Println("Client started") 160 return nil 161 } 162 163 type receiver struct { 164 signalr.Receiver 165 } 166 167 func (r *receiver) Receive(msg string) { 168 fmt.Println(msg) 169 // The silly client urges the server to end his connection after 10 seconds 170 r.Server().Send("abort") 171 } 172 173 func main() { 174 hub := &chat{} 175 176 //go runTCPServer("127.0.0.1:8007", hub) 177 go runHTTPServer("localhost:8086", hub) 178 <-time.After(time.Millisecond * 2) 179 go func() { 180 fmt.Println(runHTTPClient("http://localhost:8086/chat", &receiver{})) 181 }() 182 ch := make(chan struct{}) 183 <-ch 184 }