From 0fad1e3e34ddb95536ed11f340cf60fbbbf2b405 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Sun, 2 Nov 2014 15:44:24 +0800 Subject: [PATCH] Fix #74 --- i18n/en_US.json | 1 + i18n/ja_JP.json | 1 + i18n/zh_CN.json | 1 + i18n/zh_TW.json | 1 + main.go | 14 +++++++++-- notification/notifications.go | 3 +-- session/sessions.go | 28 ++++++++++++--------- shell/shells.go | 6 ++++- static/js/editors.js | 2 +- static/js/notification.js | 1 - static/js/session.js | 46 ++++++++++++++++++++++++++++++++--- static/js/shell.js | 1 - static/js/wide.js | 1 - views/start.html | 4 +++ 14 files changed, 86 insertions(+), 24 deletions(-) diff --git a/i18n/en_US.json b/i18n/en_US.json index e75b584..07ef97e 100644 --- a/i18n/en_US.json +++ b/i18n/en_US.json @@ -7,6 +7,7 @@ "login": "Login", "username": "Username", "current_user": "Current User", + "current_session": "Current Session", "password": "Password", "login_error": "Login Error", "run": "Run", diff --git a/i18n/ja_JP.json b/i18n/ja_JP.json index 467e944..cc33298 100644 --- a/i18n/ja_JP.json +++ b/i18n/ja_JP.json @@ -7,6 +7,7 @@ "login": "ログイン", "username": "ユーザ名", "current_user": "現在のユーザ", + "current_session": "現在のセッション", "password": "パスワード", "login_error": "ログインエラー", "run": "実行", diff --git a/i18n/zh_CN.json b/i18n/zh_CN.json index dd755b7..c56e44b 100644 --- a/i18n/zh_CN.json +++ b/i18n/zh_CN.json @@ -7,6 +7,7 @@ "login": "登录", "username": "用户名", "current_user": "当前用户", + "current_session": "当前会话", "password": "密码", "login_error": "登录失败", "run": "运行", diff --git a/i18n/zh_TW.json b/i18n/zh_TW.json index fc597c9..2a3adeb 100644 --- a/i18n/zh_TW.json +++ b/i18n/zh_TW.json @@ -7,6 +7,7 @@ "login": "登錄", "username": "用户名字", "current_user": "當前用户名", + "current_session": "當前會話", "password": "密碼", "login_error": "登錄失败", "run": "執行", diff --git a/main.go b/main.go index 1557f73..07a7306 100644 --- a/main.go +++ b/main.go @@ -3,9 +3,11 @@ package main import ( "flag" "html/template" + "math/rand" "mime" "net/http" "runtime" + "strconv" "time" "github.com/b3log/wide/conf" @@ -53,7 +55,9 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { httpSession.Save(r, w) // create a Wide session - wideSession := session.WideSessions.New(httpSession) + rand.Seed(time.Now().UnixNano()) + sid := strconv.Itoa(rand.Int()) + wideSession := session.WideSessions.New(httpSession, sid) username := httpSession.Values["username"].(string) user := conf.Wide.GetUser(username) @@ -112,8 +116,14 @@ func startHandler(w http.ResponseWriter, r *http.Request) { locale := conf.Wide.GetUser(username).Locale userWorkspace := conf.Wide.GetUserWorkspace(username) + sid := r.URL.Query()["sid"][0] + wSession := session.WideSessions.Get(sid) + if nil == wSession { + glog.Errorf("Session [%s] not found", sid) + } + model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale, - "username": username, "workspace": userWorkspace, "ver": conf.WideVersion} + "username": username, "workspace": userWorkspace, "ver": conf.WideVersion, "session": wSession} t, err := template.ParseFiles("views/start.html") diff --git a/notification/notifications.go b/notification/notifications.go index 0c6708f..d45a4b3 100644 --- a/notification/notifications.go +++ b/notification/notifications.go @@ -75,9 +75,8 @@ func WSHandler(w http.ResponseWriter, r *http.Request) { sid := r.URL.Query()["sid"][0] wSession := session.WideSessions.Get(sid) - if nil == wSession { - glog.Errorf("Session [%s] not found", sid) + if nil == wSession { return } diff --git a/session/sessions.go b/session/sessions.go index d073391..2a027e9 100644 --- a/session/sessions.go +++ b/session/sessions.go @@ -10,10 +10,8 @@ package session import ( "encoding/json" - "math/rand" "net/http" "os" - "strconv" "sync" "time" @@ -97,9 +95,18 @@ func WSHandler(w http.ResponseWriter, r *http.Request) { sid := r.URL.Query()["sid"][0] wSession := WideSessions.Get(sid) if nil == wSession { - glog.Errorf("Session [%s] not found", sid) + httpSession, _ := HTTPSession.Get(r, "wide-session") - return + if httpSession.IsNew { + return + } + + httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge + httpSession.Save(r, w) + + WideSessions.New(httpSession, sid) + + glog.Infof("Created a wide session [%s] for websocket reconnecting", sid) } conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024) @@ -107,7 +114,7 @@ func WSHandler(w http.ResponseWriter, r *http.Request) { SessionWS[sid] = &wsChan - ret := map[string]interface{}{"output": "Ouput initialized", "cmd": "init-session"} + ret := map[string]interface{}{"output": "Session initialized", "cmd": "init-session"} wsChan.Conn.WriteJSON(&ret) glog.V(4).Infof("Open a new [Session Channel] with session [%s], %d", sid, len(SessionWS)) @@ -185,20 +192,17 @@ func (s *WideSession) Refresh() { } // New creates a wide session. -func (sessions *Sessions) New(httpSession *sessions.Session) *WideSession { +func (sessions *Sessions) New(httpSession *sessions.Session, sid string) *WideSession { mutex.Lock() defer mutex.Unlock() - rand.Seed(time.Now().UnixNano()) - - id := strconv.Itoa(rand.Int()) now := time.Now() - // create user event queue - userEventQueue := event.UserEventQueues.New(id) + // create user event queuselect + userEventQueue := event.UserEventQueues.New(sid) ret := &WideSession{ - Id: id, + Id: sid, Username: httpSession.Values["username"].(string), HTTPSession: httpSession, EventQueue: userEventQueue, diff --git a/shell/shells.go b/shell/shells.go index f7bc754..41c13cf 100644 --- a/shell/shells.go +++ b/shell/shells.go @@ -3,10 +3,12 @@ package shell import ( "html/template" + "math/rand" "net/http" "os" "os/exec" "runtime" + "strconv" "strings" "time" @@ -37,7 +39,9 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { httpSession.Save(r, w) // create a wide session - wideSession := session.WideSessions.New(httpSession) + rand.Seed(time.Now().UnixNano()) + sid := strconv.Itoa(rand.Int()) + wideSession := session.WideSessions.New(httpSession, sid) username := httpSession.Values["username"].(string) locale := conf.Wide.GetUser(username).Locale diff --git a/static/js/editors.js b/static/js/editors.js index c9b84c6..f59ea4b 100644 --- a/static/js/editors.js +++ b/static/js/editors.js @@ -180,7 +180,7 @@ var editors = { title: '' + config.label.start_page + '', content: '
', after: function () { - $("#startPage").load('/start'); + $("#startPage").load('/start?sid=' + config.wideSessionId); $.ajax({ url: "http://symphony.b3log.org/apis/articles?tags=wide,golang&p=1&size=30", type: "GET", diff --git a/static/js/notification.js b/static/js/notification.js index d878d94..6c7f1a3 100644 --- a/static/js/notification.js +++ b/static/js/notification.js @@ -30,7 +30,6 @@ var notification = { notificationWS.onclose = function (e) { console.log('[notification onclose] disconnected (' + e.code + ')'); - delete notificationWS; }; notificationWS.onerror = function (e) { diff --git a/static/js/session.js b/static/js/session.js index d2fa798..b12ac1e 100644 --- a/static/js/session.js +++ b/static/js/session.js @@ -83,16 +83,56 @@ var session = { sessionWS.onopen = function () { console.log('[session onopen] connected'); + + var dateFormat = function (time, fmt) { + var date = new Date(time); + var dateObj = { + "M+": date.getMonth() + 1, //月份 + "d+": date.getDate(), //日 + "h+": date.getHours(), //小时 + "m+": date.getMinutes(), //分 + "s+": date.getSeconds(), //秒 + "q+": Math.floor((date.getMonth() + 3) / 3), //季度 + "S": date.getMilliseconds() //毫秒 + }; + if (/(y+)/.test(fmt)) + fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); + for (var k in dateObj) + if (new RegExp("(" + k + ")").test(fmt)) { + fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) + ? (dateObj[k]) : (("00" + dateObj[k]).substr(("" + dateObj[k]).length))); + } + return fmt; + }; + + var data = {type: "Network", severity: "INFO", + message: "Connected to server [sid=" + config.wideSessionId + "], " + dateFormat(new Date().getTime(), 'yyyy-MM-dd hh:mm:ss')}, + $notification = $('.bottom-window-group .notification > table'), + notificationHTML = ''; + + notificationHTML += '' + data.severity + + '' + data.message + + '' + data.type + ''; + $notification.append(notificationHTML); }; sessionWS.onmessage = function (e) { console.log('[session onmessage]' + e.data); - var data = JSON.parse(e.data); - }; sessionWS.onclose = function (e) { console.log('[session onclose] disconnected (' + e.code + ')'); - delete sessionWS; + + var data = {type: "Network", severity: "ERROR", + message: "Disconnected from server, trying to reconnect it [sid=" + config.wideSessionId + "]"}, + $notification = $('.bottom-window-group .notification > table'), + notificationHTML = ''; + + notificationHTML += '' + data.severity + + '' + data.message + + '' + data.type + ''; + $notification.append(notificationHTML); + + $(".notification-count").show(); }; sessionWS.onerror = function (e) { console.log('[session onerror] ' + JSON.parse(e)); diff --git a/static/js/shell.js b/static/js/shell.js index fb0aed6..f7cafa3 100644 --- a/static/js/shell.js +++ b/static/js/shell.js @@ -14,7 +14,6 @@ var shell = { }; shell.shellWS.onclose = function (e) { console.log('[shell onclose] disconnected (' + e.code + ')'); - delete shell.shellWS; }; shell.shellWS.onerror = function (e) { console.log('[shell onerror] ' + e); diff --git a/static/js/wide.js b/static/js/wide.js index f00dfe3..838519d 100644 --- a/static/js/wide.js +++ b/static/js/wide.js @@ -387,7 +387,6 @@ var wide = { }; outputWS.onclose = function (e) { console.log('[output onclose] disconnected (' + e.code + ')'); - delete outputWS; }; outputWS.onerror = function (e) { console.log('[output onerror] ' + e); diff --git a/views/start.html b/views/start.html index cdaf9b1..9001867 100644 --- a/views/start.html +++ b/views/start.html @@ -5,6 +5,10 @@ {{.username}} +
  • + + {{.session.Id}} +
  • {{.workspace}}