// log发送api func wsLogHandle(conn *ws.Conn) { defer func() { if p := recover(); p != nil { logs.Log.Error("%v", p) } }() var err error sess, _ := globalSessions.SessionStart(nil, conn.Request()) sessID := sess.SessionID() if Lsc.connPool[sessID] == nil { Lsc.Add(sessID, conn) } go func() { defer func() { // 关闭web前端log输出并断开websocket连接 Lsc.Remove(sessID, conn) }() for { if err := ws.JSON.Receive(conn, nil); err != nil { // logs.Log.Debug("websocket log接收出错断开 (%v) !", err) return } } }() for msg := range Lsc.lvPool[sessID].logChan { if _, err = ws.Message.Send(conn, msg); err != nil { return } } }
func wsHandle(conn *ws.Conn) { defer func() { if p := recover(); p != nil { logs.Log.Error("%v", p) } }() sess, _ := globalSessions.SessionStart(nil, conn.Request()) sessID := sess.SessionID() if Sc.GetConn(sessID) == nil { Sc.Add(sessID, conn) } defer Sc.Remove(sessID, conn) go func() { var err error for info := range Sc.GetWchan(sessID).wchan { if _, err = ws.JSON.Send(conn, info); err != nil { return } } }() for { var req map[string]interface{} if err := ws.JSON.Receive(conn, &req); err != nil { // logs.Log.Debug("websocket接收出错断开 (%v) !", err) return } // log.Log.Debug("Received from web: %v", req) wsApi[util.Atoa(req["operate"])](sessID, req) } }
func (self *LogSocketController) Remove(sessID string, conn *ws.Conn) { defer func() { recover() }() if self.connPool[sessID] == nil { return } lv := self.lvPool[sessID] lv.closed = true close(lv.logChan) conn.Close() delete(self.connPool, sessID) delete(self.lvPool, sessID) }
func (self *SocketController) Remove(sessID string, conn *ws.Conn) { self.connRWMutex.Lock() self.wchanRWMutex.Lock() defer self.connRWMutex.Unlock() defer self.wchanRWMutex.Unlock() if self.connPool[sessID] == nil { return } wc := self.wchanPool[sessID] close(wc.wchan) conn.Close() delete(self.connPool, sessID) delete(self.wchanPool, sessID) }