websocket channel enhance

This commit is contained in:
Liang Ding 2014-11-20 13:59:08 +08:00
parent 6279324854
commit d1b6a3e32d
7 changed files with 41 additions and 23 deletions

View File

@ -44,7 +44,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
editorChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
ret := map[string]interface{}{"output": "Editor initialized", "cmd": "init-editor"}
err := editorChan.Conn.WriteJSON(&ret)
err := editorChan.WriteJSON(&ret)
if nil != err {
return
}
@ -92,7 +92,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
ret = map[string]interface{}{"output": string(output.Bytes()), "cmd": "autocomplete"}
if err := session.EditorWS[sid].Conn.WriteJSON(&ret); err != nil {
if err := session.EditorWS[sid].WriteJSON(&ret); err != nil {
glog.Error("Editor WS ERROR: " + err.Error())
return
}

View File

@ -79,7 +79,7 @@ func event2Notification(e *event.Event) {
return
}
wsChannel.Conn.WriteJSON(notification)
wsChannel.WriteJSON(notification)
wsChannel.Refresh()
}
@ -98,7 +98,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
ret := map[string]interface{}{"notification": "Notification initialized", "cmd": "init-notification"}
err := wsChan.Conn.WriteJSON(&ret)
err := wsChan.WriteJSON(&ret)
if nil != err {
return
}

View File

@ -59,7 +59,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
ret := map[string]interface{}{"output": "Ouput initialized", "cmd": "init-output"}
err := wsChan.Conn.WriteJSON(&ret)
err := wsChan.WriteJSON(&ret)
if nil != err {
return
}
@ -123,7 +123,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
channelRet["cmd"] = "run-done"
channelRet["output"] = ""
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
return
@ -150,7 +150,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
if nil != wsChannel {
channelRet["cmd"] = "run"
channelRet["output"] = ""
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
return
@ -177,7 +177,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
if nil != wsChannel {
channelRet["cmd"] = "run-done"
channelRet["output"] = buf
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
break
@ -191,7 +191,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
if nil != wsChannel {
channelRet["cmd"] = "run"
channelRet["output"] = buf
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
break
@ -219,7 +219,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
channelRet["cmd"] = "run-done"
channelRet["output"] = buf
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
break
@ -235,7 +235,7 @@ func RunHandler(w http.ResponseWriter, r *http.Request) {
channelRet["cmd"] = "run"
channelRet["output"] = buf
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
break
@ -336,7 +336,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
wsChannel := session.OutputWS[sid]
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
return
@ -439,7 +439,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
glog.V(3).Infof("Session [%s] 's build [id=%d, dir=%s] has done", sid, runningId, curDir)
wsChannel := session.OutputWS[sid]
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
}
@ -508,7 +508,7 @@ func GoTestHandler(w http.ResponseWriter, r *http.Request) {
wsChannel := session.OutputWS[sid]
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
return
@ -553,7 +553,7 @@ func GoTestHandler(w http.ResponseWriter, r *http.Request) {
if nil != session.OutputWS[sid] {
wsChannel := session.OutputWS[sid]
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
}
@ -623,7 +623,7 @@ func GoInstallHandler(w http.ResponseWriter, r *http.Request) {
wsChannel := session.OutputWS[sid]
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
return
@ -712,7 +712,7 @@ func GoInstallHandler(w http.ResponseWriter, r *http.Request) {
glog.V(3).Infof("Session [%s] 's running [go install] [id=%d, dir=%s] has done", sid, runningId, curDir)
wsChannel := session.OutputWS[sid]
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
}
@ -781,7 +781,7 @@ func GoGetHandler(w http.ResponseWriter, r *http.Request) {
wsChannel := session.OutputWS[sid]
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
return
@ -825,7 +825,7 @@ func GoGetHandler(w http.ResponseWriter, r *http.Request) {
if nil != session.OutputWS[sid] {
wsChannel := session.OutputWS[sid]
err := wsChannel.Conn.WriteJSON(&channelRet)
err := wsChannel.WriteJSON(&channelRet)
if nil != err {
glog.Error(err)
}

View File

@ -127,7 +127,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
ret := map[string]interface{}{"output": "Session initialized", "cmd": "init-session"}
err := wsChan.Conn.WriteJSON(&ret)
err := wsChan.WriteJSON(&ret)
if nil != err {
return
}
@ -149,7 +149,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
ret = map[string]interface{}{"output": "", "cmd": "session-output"}
if err := wsChan.Conn.WriteJSON(&ret); err != nil {
if err := wsChan.WriteJSON(&ret); err != nil {
glog.Error("Session WS ERROR: " + err.Error())
return
}

View File

@ -90,7 +90,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
ret := map[string]interface{}{"output": "Shell initialized", "cmd": "init-shell"}
err := wsChan.Conn.WriteJSON(&ret)
err := wsChan.WriteJSON(&ret)
if nil != err {
return
}
@ -138,7 +138,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
ret = map[string]interface{}{"output": output, "cmd": "shell-output"}
if err := wsChan.Conn.WriteJSON(&ret); err != nil {
if err := wsChan.WriteJSON(&ret); err != nil {
glog.Error("Shell WS ERROR: " + err.Error())
return
}

View File

@ -37,6 +37,8 @@ var notification = {
notificationHTML = '';
if (data.cmd && "init-notification" === data.cmd) {
console.log('[notification onmessage]' + e.data);
return;
}

View File

@ -15,6 +15,7 @@
package util
import (
"errors"
"net/http"
"time"
@ -29,6 +30,21 @@ type WSChannel struct {
Time time.Time // the latest use time
}
// WriteJSON writes the JSON encoding of v to the channel.
func (c *WSChannel) WriteJSON(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.WriteJSON(v)
}
// Close closed the channel.
func (c *WSChannel) Close() {
if nil != c.Conn {