Add WebSocket server support
This commit is contained in:
parent
bbc9774a96
commit
a0d167ff75
1
go.mod
1
go.mod
@ -6,6 +6,7 @@ require (
|
|||||||
github.com/gofrs/uuid v4.2.0+incompatible
|
github.com/gofrs/uuid v4.2.0+incompatible
|
||||||
github.com/mitchellh/mapstructure v1.5.0
|
github.com/mitchellh/mapstructure v1.5.0
|
||||||
github.com/vmihailenco/msgpack/v5 v5.3.5
|
github.com/vmihailenco/msgpack/v5 v5.3.5
|
||||||
|
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4
|
||||||
)
|
)
|
||||||
|
|
||||||
require github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
require github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
|
||||||
|
2
go.sum
2
go.sum
@ -13,6 +13,8 @@ github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9
|
|||||||
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
|
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
|
||||||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
|
||||||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
|
||||||
|
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4 h1:HVyaeDAYux4pnY+D/SiwmLOR36ewZ4iGQIIrtnuCjFA=
|
||||||
|
golang.org/x/net v0.0.0-20220425223048-2871e0cb64e4/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
@ -22,12 +22,14 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"go.arsenm.dev/lrpc/codec"
|
"go.arsenm.dev/lrpc/codec"
|
||||||
"go.arsenm.dev/lrpc/internal/reflectutil"
|
"go.arsenm.dev/lrpc/internal/reflectutil"
|
||||||
"go.arsenm.dev/lrpc/internal/types"
|
"go.arsenm.dev/lrpc/internal/types"
|
||||||
|
"golang.org/x/net/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
// <= go1.17 compatibility
|
// <= go1.17 compatibility
|
||||||
@ -256,6 +258,20 @@ func (s *Server) Serve(ln net.Listener, cf codec.CodecFunc) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Server) ServeWS(addr string, cf codec.CodecFunc) (err error) {
|
||||||
|
ws := websocket.Server{}
|
||||||
|
|
||||||
|
ws.Config = websocket.Config{
|
||||||
|
Version: websocket.ProtocolVersionHybi13,
|
||||||
|
}
|
||||||
|
|
||||||
|
ws.Handler = func(c *websocket.Conn) {
|
||||||
|
s.handleConn(cf(c))
|
||||||
|
}
|
||||||
|
|
||||||
|
return http.ListenAndServe(addr, http.HandlerFunc(ws.ServeHTTP))
|
||||||
|
}
|
||||||
|
|
||||||
// handleConn handles a listener connection
|
// handleConn handles a listener connection
|
||||||
func (s *Server) handleConn(c codec.Codec) {
|
func (s *Server) handleConn(c codec.Codec) {
|
||||||
for {
|
for {
|
||||||
|
Reference in New Issue
Block a user