diff --git a/editor/editors.go b/editor/editors.go index 83a9ac9..5945f33 100644 --- a/editor/editors.go +++ b/editor/editors.go @@ -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 } diff --git a/notification/notifications.go b/notification/notifications.go index d8b39e7..3c557ba 100644 --- a/notification/notifications.go +++ b/notification/notifications.go @@ -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 } diff --git a/output/outputs.go b/output/outputs.go index 0bde0de..4cd4b59 100644 --- a/output/outputs.go +++ b/output/outputs.go @@ -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) } diff --git a/session/sessions.go b/session/sessions.go index a4df297..3c5d434 100644 --- a/session/sessions.go +++ b/session/sessions.go @@ -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 } diff --git a/shell/shells.go b/shell/shells.go index 3485e48..9615c06 100644 --- a/shell/shells.go +++ b/shell/shells.go @@ -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 } diff --git a/static/js/notification.js b/static/js/notification.js index 2b0fdc3..36de39f 100644 --- a/static/js/notification.js +++ b/static/js/notification.js @@ -37,6 +37,8 @@ var notification = { notificationHTML = ''; if (data.cmd && "init-notification" === data.cmd) { + console.log('[notification onmessage]' + e.data); + return; } diff --git a/util/websocket.go b/util/websocket.go index a36d3c2..0781920 100644 --- a/util/websocket.go +++ b/util/websocket.go @@ -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 {