close channel on server side
This commit is contained in:
parent
9ad754ba04
commit
2c44953b9b
|
@ -62,5 +62,5 @@ func Dial(url string, tr transport.Transport) (*Client, error) {
|
||||||
Close client connection
|
Close client connection
|
||||||
*/
|
*/
|
||||||
func (c *Client) Close() {
|
func (c *Client) Close() {
|
||||||
CloseChannel(&c.Channel, &c.methods)
|
closeChannel(&c.Channel, &c.methods)
|
||||||
}
|
}
|
||||||
|
|
12
loop.go
12
loop.go
|
@ -83,7 +83,7 @@ func (c *Channel) IsAlive() bool {
|
||||||
/**
|
/**
|
||||||
Close channel
|
Close channel
|
||||||
*/
|
*/
|
||||||
func CloseChannel(c *Channel, m *methods, args ...interface{}) error {
|
func closeChannel(c *Channel, m *methods, args ...interface{}) error {
|
||||||
c.aliveLock.Lock()
|
c.aliveLock.Lock()
|
||||||
defer c.aliveLock.Unlock()
|
defer c.aliveLock.Unlock()
|
||||||
|
|
||||||
|
@ -115,18 +115,18 @@ func inLoop(c *Channel, m *methods) error {
|
||||||
for {
|
for {
|
||||||
pkg, err := c.conn.GetMessage()
|
pkg, err := c.conn.GetMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return CloseChannel(c, m, err)
|
return closeChannel(c, m, err)
|
||||||
}
|
}
|
||||||
msg, err := protocol.Decode(pkg)
|
msg, err := protocol.Decode(pkg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
CloseChannel(c, m, protocol.ErrorWrongPacket)
|
closeChannel(c, m, protocol.ErrorWrongPacket)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch msg.Type {
|
switch msg.Type {
|
||||||
case protocol.MessageTypeOpen:
|
case protocol.MessageTypeOpen:
|
||||||
if err := json.Unmarshal([]byte(msg.Source[1:]), &c.header); err != nil {
|
if err := json.Unmarshal([]byte(msg.Source[1:]), &c.header); err != nil {
|
||||||
CloseChannel(c, m, ErrorWrongHeader)
|
closeChannel(c, m, ErrorWrongHeader)
|
||||||
}
|
}
|
||||||
m.callLoopEvent(c, OnConnection)
|
m.callLoopEvent(c, OnConnection)
|
||||||
case protocol.MessageTypePing:
|
case protocol.MessageTypePing:
|
||||||
|
@ -156,7 +156,7 @@ func outLoop(c *Channel, m *methods) error {
|
||||||
for {
|
for {
|
||||||
outBufferLen := len(c.out)
|
outBufferLen := len(c.out)
|
||||||
if outBufferLen >= queueBufferSize-1 {
|
if outBufferLen >= queueBufferSize-1 {
|
||||||
return CloseChannel(c, m, ErrorSocketOverflood)
|
return closeChannel(c, m, ErrorSocketOverflood)
|
||||||
} else if outBufferLen > int(queueBufferSize/2) {
|
} else if outBufferLen > int(queueBufferSize/2) {
|
||||||
overfloodedLock.Lock()
|
overfloodedLock.Lock()
|
||||||
overflooded[c] = struct{}{}
|
overflooded[c] = struct{}{}
|
||||||
|
@ -174,7 +174,7 @@ func outLoop(c *Channel, m *methods) error {
|
||||||
|
|
||||||
err := c.conn.WriteMessage(msg)
|
err := c.conn.WriteMessage(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return CloseChannel(c, m, err)
|
return closeChannel(c, m, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in New Issue