2017-11-29 09:55:24 +00:00
|
|
|
package fasthttp
|
|
|
|
|
|
|
|
import (
|
2017-11-29 11:17:01 +00:00
|
|
|
"io"
|
2017-11-29 09:55:24 +00:00
|
|
|
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2017-11-29 10:02:33 +00:00
|
|
|
func New() client.ClientReadWriteCloseHandler {
|
|
|
|
return &ClientReadWriteCloseHandlers{}
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:55:24 +00:00
|
|
|
type ClientReadWriteCloseHandlers struct {
|
|
|
|
client.ClientReadWriteCloseHandlers
|
|
|
|
}
|
|
|
|
|
2017-11-30 03:36:03 +00:00
|
|
|
func (crwch *ClientReadWriteCloseHandlers) ReadResponse(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}) (protocol.ClientResponseCodec, error) {
|
2017-11-29 11:17:01 +00:00
|
|
|
if nil == conn {
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:55:24 +00:00
|
|
|
soc := conn.(cwf.Socket)
|
|
|
|
_, r, err := soc.NextReader()
|
|
|
|
|
|
|
|
resCodec, err := codec.NewResponse(r)
|
|
|
|
|
|
|
|
return resCodec, err
|
|
|
|
}
|
|
|
|
|
2017-11-30 03:36:03 +00:00
|
|
|
func (crwch *ClientReadWriteCloseHandlers) WriteRequest(clientCTX client.ClientContext, codec protocol.ClientCodec, conn interface{}, method string, params interface{}, id interface{}) error {
|
2017-11-29 11:17:01 +00:00
|
|
|
if nil == conn {
|
|
|
|
return io.EOF
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:55:24 +00:00
|
|
|
soc := conn.(cwf.Socket)
|
|
|
|
|
|
|
|
wc, wErr := soc.NextWriter(websocket.TextMessage)
|
|
|
|
if nil != wErr {
|
|
|
|
return wErr
|
|
|
|
}
|
2017-11-29 11:17:01 +00:00
|
|
|
defer func() {
|
|
|
|
wc.Close()
|
|
|
|
}()
|
2017-11-29 09:55:24 +00:00
|
|
|
|
|
|
|
if wErr := codec.WriteRequest(wc, method, params); nil != wErr {
|
|
|
|
return wErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-30 03:36:03 +00:00
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Disconnect(clientCTX client.ClientContext, conn interface{}) {
|
2017-11-29 11:17:01 +00:00
|
|
|
if nil == conn {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-29 09:55:24 +00:00
|
|
|
soc := conn.(cwf.Socket)
|
|
|
|
soc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (crwch *ClientReadWriteCloseHandlers) Validate() {
|
|
|
|
|
|
|
|
}
|