🔥 channel enhance

This commit is contained in:
Liang Ding 2014-11-20 14:11:54 +08:00
parent f006e6bfb4
commit e6607b3bef
5 changed files with 21 additions and 4 deletions

View File

@ -55,7 +55,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
args := map[string]interface{}{}
for {
if err := session.EditorWS[sid].Conn.ReadJSON(&args); err != nil {
if err := session.EditorWS[sid].ReadJSON(&args); err != nil {
if err.Error() == "EOF" {
return
}
@ -65,6 +65,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
}
glog.Error("Editor WS ERROR: " + err.Error())
return
}

View File

@ -113,7 +113,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
input := map[string]interface{}{}
for {
if err := wsChan.Conn.ReadJSON(&input); err != nil {
if err := wsChan.ReadJSON(&input); err != nil {
if err.Error() == "EOF" {
return
}

View File

@ -139,7 +139,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
input := map[string]interface{}{}
for {
if err := wsChan.Conn.ReadJSON(&input); err != nil {
if err := wsChan.ReadJSON(&input); err != nil {
glog.V(3).Infof("[Session Channel] of session [%s] disconnected, releases all resources with it", sid)
WideSessions.Remove(sid)

View File

@ -102,7 +102,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
input := map[string]interface{}{}
for {
if err := wsChan.Conn.ReadJSON(&input); err != nil {
if err := wsChan.ReadJSON(&input); err != nil {
if err.Error() == "EOF" {
return
}
@ -112,6 +112,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
}
glog.Error("Shell WS ERROR: " + err.Error())
return
}

View File

@ -45,6 +45,21 @@ func (c *WSChannel) WriteJSON(v interface{}) (ret error) {
return c.Conn.WriteJSON(v)
}
// ReadJSON reads the next JSON-encoded message from the channel and stores it in the value pointed to by v.
func (c *WSChannel) ReadJSON(v interface{}) (ret error) {
if nil == c.Conn {
return errors.New("connection is nil, channel has been closed")
}
defer func() {
if r := recover(); nil != r {
ret = errors.New("channel has been closed")
}
}()
return c.Conn.ReadJSON(v)
}
// Close closed the channel.
func (c *WSChannel) Close() {
if nil != c.Conn {