Add WebSocket server support

This commit is contained in:
2022-05-02 14:47:00 -07:00
parent bbc9774a96
commit a0d167ff75
3 changed files with 19 additions and 0 deletions

View File

@@ -22,12 +22,14 @@ import (
"errors"
"io"
"net"
"net/http"
"reflect"
"sync"
"go.arsenm.dev/lrpc/codec"
"go.arsenm.dev/lrpc/internal/reflectutil"
"go.arsenm.dev/lrpc/internal/types"
"golang.org/x/net/websocket"
)
// <= 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
func (s *Server) handleConn(c codec.Codec) {
for {