Skip to content

Commit

Permalink
修复: websocket在读线程关闭session导致的写入并发 davyxu#26
Browse files Browse the repository at this point in the history
  • Loading branch information
davyxu committed Nov 16, 2017
1 parent 6635992 commit 59e173f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 4 additions & 1 deletion examples/websocket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/davyxu/golog"
)

var log *golog.Logger = golog.New("main")
var log = golog.New("main")

// 运行服务器, 在浏览器(Chrome)中打开index.html, F12打开调试窗口->Console标签 查看命令行输出
func main() {
Expand All @@ -31,6 +31,9 @@ func main() {
log.Debugln(msg.Content)

ev.Send(&jsongamedef.TestEchoJsonACK{Content: "roger"})

ev.Ses.Close()

})

queue.StartLoop()
Expand Down
8 changes: 6 additions & 2 deletions websocket/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func (self *wsSession) FromPeer() cellnet.Peer {
}

func (self *wsSession) Close() {

self.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
self.sendChan <- nil
}

func (self *wsSession) Send(data interface{}) {
Expand Down Expand Up @@ -77,6 +76,11 @@ func (self *wsSession) sendThread() {

for ev := range self.sendChan {

if ev == nil {
self.conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
break
}

meta := cellnet.MessageMetaByID(ev.MsgID)

if meta == nil {
Expand Down

0 comments on commit 59e173f

Please sign in to comment.