ws/cmd/iface/main.go

73 lines
1.4 KiB
Go

package main
import (
"git.xdrm.io/gws/ws/message"
"git.xdrm.io/gws/ws/controller"
"time"
"git.xdrm.io/gws/ws/server"
"fmt"
)
func main(){
startTime := time.Now().UnixNano()
/* (1) Bind WebSocket server */
serv := server.Create("0.0.0.0", 4444)
/* (2) Bind default controller */
serv.BindDefault(func(client *controller.ClientInterface, receiver <-chan message.T, sender chan<- message.T, closer <-chan func()){
for {
select {
// if receive message -> send it back
case msg := <- receiver:
sender <- msg
// if received closer, close after doing stuff
case closeCallback := <- closer:
closeCallback()
break
}
}
})
/* (3) Bind to URI */
err := serv.Bind("/channel/./room/./", func(client *controller.ClientInterface, receiver <-chan message.T, sender chan<- message.T, closer <-chan func()){
fmt.Printf("[uri] connected\n")
for {
select {
case msg := <- receiver:
fmt.Printf("[uri] received '%s'\n", msg.Data)
sender <- msg
case closeFunc := <- closer:
fmt.Printf("[uri] client with protocol '%s' exited\n", client.Protocol)
closeFunc()
return
}
}
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)
}