2018-04-26 20:54:17 +00:00
|
|
|
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 */
|
2018-04-28 15:51:05 +00:00
|
|
|
serv.BindDefault(func(client *ws.Client, receiver <-chan *ws.Frame, sender chan<- []byte, closer <-chan func()){
|
2018-04-28 14:06:46 +00:00
|
|
|
|
|
|
|
fmt.Printf("[default] connected\n")
|
2018-04-28 15:51:05 +00:00
|
|
|
for {
|
|
|
|
|
|
|
|
select {
|
|
|
|
case receivedFrame := <- receiver:
|
|
|
|
fmt.Printf("[default] received '%s'\n", receivedFrame.Payload.Buffer)
|
|
|
|
sender <- receivedFrame.Payload.Buffer[1:]
|
|
|
|
case closeFunc := <- closer:
|
|
|
|
fmt.Printf("[default] client with protocol '%s' exited\n", client.Protocol)
|
|
|
|
closeFunc()
|
|
|
|
return
|
|
|
|
}
|
2018-04-27 10:16:34 +00:00
|
|
|
}
|
2018-04-28 15:51:05 +00:00
|
|
|
|
|
|
|
fmt.Printf("[default] unexpectedly closed\n")
|
2018-04-28 14:06:46 +00:00
|
|
|
|
2018-04-26 20:54:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
/* (3) Bind to URI */
|
2018-04-28 15:51:05 +00:00
|
|
|
err := serv.Bind("/channel/./room/./", func(client *ws.Client, receiver <-chan *ws.Frame, sender chan<- []byte, closer <-chan func()){
|
2018-04-28 14:06:46 +00:00
|
|
|
|
|
|
|
fmt.Printf("[uri] connected\n")
|
2018-04-28 15:51:05 +00:00
|
|
|
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
|
|
|
|
}
|
2018-04-27 10:16:34 +00:00
|
|
|
}
|
2018-04-28 15:51:05 +00:00
|
|
|
fmt.Printf("[uri] unexpectedly closed\n")
|
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)
|
|
|
|
|
|
|
|
}
|