ws/cmd/iface/main.go

56 lines
1.1 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"time"
2021-05-14 14:47:02 +00:00
ws "git.xdrm.io/go/ws"
)
func main() {
startTime := time.Now().UnixNano()
// creqte WebSocket server
serv := ws.NewServer("0.0.0.0", 4444)
// bind default controller
serv.BindDefault(func(cli *ws.Client, receiver <-chan ws.Message, sender chan<- ws.Message, bc chan<- ws.Message) {
defer func() {
if recover() != nil {
2018-05-05 23:05:52 +00:00
fmt.Printf("*** PANIC\n")
}
}()
for msg := range receiver {
// if receive message -> send it back
sender <- msg
// close(sender)
}
})
// bnd to URI
err := serv.Bind("/channel/./room/./", func(cli *ws.Client, receiver <-chan ws.Message, sender chan<- ws.Message, bc chan<- ws.Message) {
fmt.Printf("[uri] connected\n")
for msg := range receiver {
fmt.Printf("[uri] received '%s'\n", msg.Data)
sender <- msg
}
fmt.Printf("[uri] unexpectedly closed\n")
})
if err != nil {
panic(err)
}
// launch the server
2018-04-26 21:40:53 +00:00
err = serv.Launch()
if err != nil {
fmt.Printf("[ERROR] %s\n", err)
return
}
fmt.Printf("+ elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
}