ing
This commit is contained in:
parent
24f7b3d3d6
commit
53b01d7965
15
client.go
15
client.go
@ -11,30 +11,29 @@ import (
|
||||
"git.loafle.net/overflow/overflow_gateway_websocket/protocol"
|
||||
|
||||
"git.loafle.net/overflow/overflow_gateway_websocket/websocket"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
ID() string
|
||||
Conn() *websocket.Conn
|
||||
RequestCtx() *fasthttp.RequestCtx
|
||||
Path() string
|
||||
}
|
||||
|
||||
type client struct {
|
||||
id string
|
||||
o *ClientOptions
|
||||
fastHTTPCtx *fasthttp.RequestCtx
|
||||
conn *websocket.Conn
|
||||
path string
|
||||
messageType int
|
||||
writeMTX sync.Mutex
|
||||
}
|
||||
|
||||
func NewClient(id string, o *ClientOptions, ctx *fasthttp.RequestCtx, conn *websocket.Conn) Client {
|
||||
func NewClient(id string, path string, o *ClientOptions, conn *websocket.Conn) Client {
|
||||
c := &client{
|
||||
id: id,
|
||||
o: o,
|
||||
fastHTTPCtx: ctx,
|
||||
conn: conn,
|
||||
path: path,
|
||||
messageType: websocket.TextMessage,
|
||||
}
|
||||
|
||||
@ -51,8 +50,8 @@ func (c *client) Conn() *websocket.Conn {
|
||||
return c.conn
|
||||
}
|
||||
|
||||
func (c *client) RequestCtx() *fasthttp.RequestCtx {
|
||||
return c.fastHTTPCtx
|
||||
func (c *client) Path() string {
|
||||
return c.path
|
||||
}
|
||||
|
||||
func (c *client) run() {
|
||||
@ -62,8 +61,6 @@ func (c *client) run() {
|
||||
c.o.OnDisconnected(c)
|
||||
}()
|
||||
|
||||
log.Printf("Path:%s", string(c.fastHTTPCtx.Path()))
|
||||
|
||||
for {
|
||||
if hasReadTimeout {
|
||||
c.conn.SetReadDeadline(time.Now().Add(c.o.ReadTimeout))
|
||||
|
@ -2,6 +2,7 @@ package overflow_gateway_websocket
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.loafle.net/overflow/overflow_gateway_websocket/websocket"
|
||||
"github.com/valyala/fasthttp"
|
||||
@ -60,12 +61,16 @@ func (s *server) onConnection(ctx *fasthttp.RequestCtx) {
|
||||
}
|
||||
co, ok := s._handlers[path]
|
||||
if !ok {
|
||||
ctx.Response.Header.Set("Sec-Websocket-Version", "13")
|
||||
ctx.Error(http.StatusText(fasthttp.StatusNotFound), fasthttp.StatusNotFound)
|
||||
|
||||
log.Printf("Path[%s] is not exist.", path)
|
||||
conn.Close()
|
||||
return
|
||||
}
|
||||
|
||||
cid := s._option.IDGenerator(ctx)
|
||||
c := NewClient(cid, co, ctx, conn)
|
||||
c := NewClient(cid, path, co, conn)
|
||||
s._clients[cid] = c
|
||||
s._option.OnConnection(path, c)
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user