rpc/client/rwc/websocket/fasthttp/client_handlers.go
crusader 74d87608c6 ing
2017-11-29 20:17:01 +09:00

73 lines
1.5 KiB
Go

package fasthttp
import (
"fmt"
"io"
"github.com/gorilla/websocket"
"git.loafle.net/commons_go/rpc/client"
"git.loafle.net/commons_go/rpc/protocol"
cwf "git.loafle.net/commons_go/websocket_fasthttp"
)
func New() client.ClientReadWriteCloseHandler {
return &ClientReadWriteCloseHandlers{}
}
type ClientReadWriteCloseHandlers struct {
client.ClientReadWriteCloseHandlers
}
func (crwch *ClientReadWriteCloseHandlers) Connect() (interface{}, error) {
return nil, fmt.Errorf("RPC Client RWC Handler: ClientHandlers method[Connect] is not implement")
}
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
if nil == conn {
return nil, io.EOF
}
soc := conn.(cwf.Socket)
_, r, err := soc.NextReader()
resCodec, err := codec.NewResponse(r)
return resCodec, err
}
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params interface{}, id interface{}) error {
if nil == conn {
return io.EOF
}
soc := conn.(cwf.Socket)
wc, wErr := soc.NextWriter(websocket.TextMessage)
if nil != wErr {
return wErr
}
defer func() {
wc.Close()
}()
if wErr := codec.WriteRequest(wc, method, params); nil != wErr {
return wErr
}
return nil
}
func (crwch *ClientReadWriteCloseHandlers) Disconnect(conn interface{}) {
if nil == conn {
return
}
soc := conn.(cwf.Socket)
soc.Close()
}
func (crwch *ClientReadWriteCloseHandlers) Validate() {
}