You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
nnet/test/test_websocket.go

70 lines
1.6 KiB
Go

package main
import (
"git.noahlan.cn/noahlan/nnet"
"git.noahlan.cn/noahlan/nnet/config"
"git.noahlan.cn/noahlan/nnet/conn"
"git.noahlan.cn/noahlan/nnet/packet"
"git.noahlan.cn/noahlan/nnet/protocol/plain"
rt "git.noahlan.cn/noahlan/nnet/router"
"git.noahlan.cn/noahlan/ntool/nlog"
"time"
)
func runWSServer(addr, path string) {
nginOpts := make([]nnet.RunOption, 0)
nginOpts = append(nginOpts, plain.WithPlainProtocol()...)
ngin := nnet.NewEngine(config.EngineConf{
Mode: config.DevMode,
Name: "DevNL",
}, nginOpts...)
ngin.AddRoutes(rt.Route{
Matches: nil,
Handler: func(conn *conn.Connection, pkg packet.IPacket) {
nlog.Debugf("route fn: %v", pkg)
},
})
defer ngin.Stop()
err := ngin.ListenWebsocket(config.WSServerFullConf{
WSConf: config.WSConf{
Addr: addr,
Path: path,
HandshakeTimeout: time.Second * 5,
},
WSEvent: config.WSEvent{},
}, nil)
if err != nil {
return
}
}
func runWSClient(addr string) (*nnet.Engine, *conn.Connection) {
//chReady := make(chan struct{})
nginOpts := make([]nnet.RunOption, 0)
nginOpts = append(nginOpts, plain.WithPlainProtocol()...)
//var onReadyFn event.OnConnectedFn = func(nc *conn.Connection) {
// chReady <- struct{}{}
//}
//nginOpts = append(nginOpts, func(ngin *nnet.Engine) {
// ngin.EventManager().RegisterEvent(event.EvtOnConnected, onReadyFn)
//})
ngin := nnet.NewEngine(config.EngineConf{
Mode: config.DevMode,
Name: "DevNL-Client",
}, nginOpts...)
nc, err := ngin.DialWebsocket(addr, config.WSClientFullConf{})
nlog.Must(err)
return ngin, nc
}