70 lines
1.5 KiB
Go
70 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"time"
|
|
"git.xdrm.io/gws/ws"
|
|
"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(client *ws.Client, receiver <-chan *ws.Frame, sender chan<- []byte, closer <-chan func()){
|
|
|
|
fmt.Printf("[default] connected\n")
|
|
for {
|
|
|
|
select {
|
|
case receivedFrame := <- receiver:
|
|
fmt.Printf("[default] received '%s'\n", receivedFrame.Payload.Buffer)
|
|
if receivedFrame.Payload.Length > 0 {
|
|
sender <- receivedFrame.Payload.Buffer[1:]
|
|
}
|
|
case closeFunc := <- closer:
|
|
fmt.Printf("[default] client with protocol '%s' exited\n", client.Protocol)
|
|
closeFunc()
|
|
return
|
|
}
|
|
}
|
|
|
|
fmt.Printf("[default] unexpectedly closed\n")
|
|
|
|
})
|
|
|
|
/* (3) Bind to URI */
|
|
err := serv.Bind("/channel/./room/./", func(client *ws.Client, receiver <-chan *ws.Frame, sender chan<- []byte, closer <-chan func()){
|
|
|
|
fmt.Printf("[uri] connected\n")
|
|
for {
|
|
|
|
select {
|
|
case receivedFrame := <- receiver:
|
|
fmt.Printf("[uri] received '%s'\n", receivedFrame.Payload.Buffer)
|
|
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)
|
|
|
|
} |