2021-06-15 22:04:09 +00:00
|
|
|
package main
|
2018-04-26 20:54:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-29 12:36:47 +00:00
|
|
|
"time"
|
2021-05-14 14:47:02 +00:00
|
|
|
|
|
|
|
ws "git.xdrm.io/go/ws"
|
2018-04-26 20:54:17 +00:00
|
|
|
)
|
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
func main() {
|
2018-04-26 20:54:17 +00:00
|
|
|
|
|
|
|
startTime := time.Now().UnixNano()
|
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// creqte WebSocket server
|
|
|
|
serv := ws.NewServer("0.0.0.0", 4444)
|
2018-04-26 20:54:17 +00:00
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// bind default controller
|
2018-09-29 12:36:47 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
for msg := range receiver {
|
|
|
|
// if receive message -> send it back
|
2018-05-05 16:43:16 +00:00
|
|
|
sender <- msg
|
|
|
|
// close(sender)
|
2018-05-03 19:10:02 +00:00
|
|
|
}
|
2018-04-26 20:54:17 +00:00
|
|
|
})
|
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// bnd to URI
|
2018-09-29 12:36:47 +00:00
|
|
|
err := serv.Bind("/channel/./room/./", func(cli *ws.Client, receiver <-chan ws.Message, sender chan<- ws.Message, bc chan<- ws.Message) {
|
2018-05-03 19:10:02 +00:00
|
|
|
fmt.Printf("[uri] connected\n")
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-09-29 12:36:47 +00:00
|
|
|
for msg := range receiver {
|
2018-05-03 19:10:02 +00:00
|
|
|
fmt.Printf("[uri] received '%s'\n", msg.Data)
|
2018-05-05 16:43:16 +00:00
|
|
|
sender <- msg
|
2018-05-03 17:31:09 +00:00
|
|
|
|
2018-05-03 19:10:02 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("[uri] unexpectedly closed\n")
|
2018-04-26 20:54:17 +00:00
|
|
|
})
|
2018-09-29 12:36:47 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-04-26 20:54:17 +00:00
|
|
|
|
2021-06-15 22:04:09 +00:00
|
|
|
// launch the server
|
2018-04-26 21:40:53 +00:00
|
|
|
err = serv.Launch()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[ERROR] %s\n", err)
|
|
|
|
return
|
|
|
|
}
|
2018-04-26 20:54:17 +00:00
|
|
|
|
|
|
|
fmt.Printf("+ elapsed: %1.1f us\n", float32(time.Now().UnixNano()-startTime)/1e3)
|
2018-09-29 12:36:47 +00:00
|
|
|
}
|