2018-04-26 20:54:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-05-03 17:31:09 +00:00
|
|
|
"git.xdrm.io/gws/ws"
|
2018-04-26 20:54:17 +00:00
|
|
|
"time"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func main(){
|
|
|
|
|
|
|
|
startTime := time.Now().UnixNano()
|
|
|
|
|
|
|
|
/* (1) Bind WebSocket server */
|
2018-05-03 17:31:09 +00:00
|
|
|
serv := ws.CreateServer("0.0.0.0", 4444)
|
2018-04-26 20:54:17 +00:00
|
|
|
|
|
|
|
/* (2) Bind default controller */
|
2018-05-03 17:31:09 +00:00
|
|
|
serv.BindDefault(func(cli *ws.Client, receiver <-chan ws.Message, sender chan<- *ws.Message, bc chan<- *ws.Message) chan struct{}{
|
2018-04-28 14:06:46 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
var closer chan struct{} = make(chan struct{}, 1)
|
2018-04-28 15:51:05 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
go func(){
|
|
|
|
for msg := range receiver {
|
2018-05-02 20:56:38 +00:00
|
|
|
|
|
|
|
// if receive message -> send it back
|
2018-05-03 17:31:09 +00:00
|
|
|
sender <- &msg
|
2018-05-02 20:56:38 +00:00
|
|
|
|
2018-04-28 15:51:05 +00:00
|
|
|
}
|
2018-05-03 17:31:09 +00:00
|
|
|
}()
|
2018-04-28 15:51:05 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
return closer
|
2018-04-28 14:06:46 +00:00
|
|
|
|
2018-04-26 20:54:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
/* (3) Bind to URI */
|
2018-05-03 17:31:09 +00:00
|
|
|
err := serv.Bind("/channel/./room/./", func(cli *ws.Client, receiver <-chan ws.Message, sender chan<- *ws.Message, bc chan<- *ws.Message) chan struct{}{
|
|
|
|
|
|
|
|
var closer chan struct{} = make(chan struct{}, 1)
|
|
|
|
|
|
|
|
go func(){
|
|
|
|
fmt.Printf("[uri] connected\n")
|
|
|
|
|
|
|
|
for msg := range receiver{
|
|
|
|
|
|
|
|
fmt.Printf("[uri] received '%s'\n", msg.Data)
|
|
|
|
sender <- &msg
|
|
|
|
|
2018-04-28 15:51:05 +00:00
|
|
|
}
|
2018-04-30 13:35:30 +00:00
|
|
|
|
2018-05-03 17:31:09 +00:00
|
|
|
fmt.Printf("[uri] unexpectedly closed\n")
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
|
|
return closer
|
2018-04-28 14:06:46 +00:00
|
|
|
|
2018-04-26 20:54:17 +00:00
|
|
|
})
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
|
|
|
/* (4) 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)
|
|
|
|
|
|
|
|
}
|