57 lines
1.0 KiB
Go
57 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"git.xdrm.io/gws/ws"
|
|
"time"
|
|
"fmt"
|
|
)
|
|
|
|
|
|
func main(){
|
|
|
|
startTime := time.Now().UnixNano()
|
|
|
|
/* (1) Bind WebSocket server */
|
|
serv := ws.CreateServer("0.0.0.0", 4444)
|
|
|
|
/* (2) Bind default controller */
|
|
serv.BindDefault(func(cli *ws.Client, receiver <-chan ws.Message, sender chan<- *ws.Message, bc chan<- *ws.Message){
|
|
|
|
for msg := range receiver {
|
|
|
|
// if receive message -> send it back
|
|
sender <- &msg
|
|
sender <- nil
|
|
|
|
}
|
|
|
|
})
|
|
|
|
/* (3) Bind 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) }
|
|
|
|
/* (4) Launch the server */
|
|
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)
|
|
|
|
} |