167 lines
5.5 KiB
Go
167 lines
5.5 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
"time"
|
||
|
|
||
|
"github.com/lesismal/llib/std/crypto/tls"
|
||
|
"github.com/lesismal/nbio/nbhttp"
|
||
|
"github.com/lesismal/nbio/nbhttp/websocket"
|
||
|
"github.com/lesismal/nbio/taskpool"
|
||
|
)
|
||
|
|
||
|
func newUpgrader(isDataFrame bool) *websocket.Upgrader {
|
||
|
u := websocket.NewUpgrader()
|
||
|
u.EnableCompression(true)
|
||
|
if isDataFrame {
|
||
|
isFirst := true
|
||
|
u.OnDataFrame(func(c *websocket.Conn, messageType websocket.MessageType, fin bool, data []byte) {
|
||
|
err := c.WriteFrame(messageType, isFirst, fin, data)
|
||
|
if err != nil {
|
||
|
c.Close()
|
||
|
return
|
||
|
}
|
||
|
if fin {
|
||
|
isFirst = true
|
||
|
} else {
|
||
|
isFirst = false
|
||
|
}
|
||
|
})
|
||
|
} else {
|
||
|
u.OnMessage(func(c *websocket.Conn, messageType websocket.MessageType, data []byte) {
|
||
|
c.WriteMessage(messageType, data)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
return u
|
||
|
}
|
||
|
|
||
|
func onWebsocketFrame(w http.ResponseWriter, r *http.Request) {
|
||
|
upgrader := newUpgrader(true)
|
||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
conn.SetDeadline(time.Time{})
|
||
|
}
|
||
|
|
||
|
func onWebsocketMessage(w http.ResponseWriter, r *http.Request) {
|
||
|
upgrader := newUpgrader(false)
|
||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
conn.SetDeadline(time.Time{})
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
cert, err := tls.X509KeyPair(rsaCertPEM, rsaKeyPEM)
|
||
|
if err != nil {
|
||
|
log.Fatalf("tls.X509KeyPair failed: %v", err)
|
||
|
}
|
||
|
tlsConfig := &tls.Config{
|
||
|
Certificates: []tls.Certificate{cert},
|
||
|
InsecureSkipVerify: true,
|
||
|
}
|
||
|
|
||
|
mux := &http.ServeMux{}
|
||
|
mux.HandleFunc("/echo/message", onWebsocketMessage)
|
||
|
mux.HandleFunc("/echo/frame", onWebsocketFrame)
|
||
|
|
||
|
log.Printf("calling new server tls\n")
|
||
|
|
||
|
messageHandlerExecutePool := taskpool.NewFixedPool(100, 1000)
|
||
|
svrTLS := nbhttp.NewServer(nbhttp.Config{
|
||
|
Network: "tcp",
|
||
|
AddrsTLS: []string{"localhost:9999"},
|
||
|
TLSConfig: tlsConfig,
|
||
|
ReadBufferSize: 1024 * 1024,
|
||
|
Handler: mux,
|
||
|
ServerExecutor: messageHandlerExecutePool.Go,
|
||
|
})
|
||
|
svr := nbhttp.NewServer(nbhttp.Config{
|
||
|
Network: "tcp",
|
||
|
Addrs: []string{"localhost:9998"},
|
||
|
ReadBufferSize: 1024 * 1024,
|
||
|
Handler: mux,
|
||
|
ServerExecutor: messageHandlerExecutePool.Go,
|
||
|
})
|
||
|
|
||
|
log.Printf("calling start non-tls\n")
|
||
|
err = svr.Start()
|
||
|
if err != nil {
|
||
|
fmt.Printf("nbio.Start non-tls failed: %v\n", err)
|
||
|
return
|
||
|
}
|
||
|
defer svr.Stop()
|
||
|
|
||
|
log.Printf("calling start tls\n")
|
||
|
err = svrTLS.Start()
|
||
|
if err != nil {
|
||
|
fmt.Printf("nbio.Start tls failed: %v\n", err)
|
||
|
return
|
||
|
}
|
||
|
defer svrTLS.Stop()
|
||
|
|
||
|
interrupt := make(chan os.Signal, 1)
|
||
|
signal.Notify(interrupt, os.Interrupt)
|
||
|
<-interrupt
|
||
|
log.Println("exit")
|
||
|
}
|
||
|
|
||
|
var rsaCertPEM = []byte(`-----BEGIN CERTIFICATE-----
|
||
|
MIIDazCCAl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