This commit is contained in:
Liang Ding 2014-11-02 15:44:24 +08:00
parent 91a8fdf871
commit 0fad1e3e34
14 changed files with 86 additions and 24 deletions

View File

@ -7,6 +7,7 @@
"login": "Login", "login": "Login",
"username": "Username", "username": "Username",
"current_user": "Current User", "current_user": "Current User",
"current_session": "Current Session",
"password": "Password", "password": "Password",
"login_error": "Login Error", "login_error": "Login Error",
"run": "Run", "run": "Run",

View File

@ -7,6 +7,7 @@
"login": "ログイン", "login": "ログイン",
"username": "ユーザ名", "username": "ユーザ名",
"current_user": "現在のユーザ", "current_user": "現在のユーザ",
"current_session": "現在のセッション",
"password": "パスワード", "password": "パスワード",
"login_error": "ログインエラー", "login_error": "ログインエラー",
"run": "実行", "run": "実行",

View File

@ -7,6 +7,7 @@
"login": "登录", "login": "登录",
"username": "用户名", "username": "用户名",
"current_user": "当前用户", "current_user": "当前用户",
"current_session": "当前会话",
"password": "密码", "password": "密码",
"login_error": "登录失败", "login_error": "登录失败",
"run": "运行", "run": "运行",

View File

@ -7,6 +7,7 @@
"login": "登錄", "login": "登錄",
"username": "用户名字", "username": "用户名字",
"current_user": "當前用户名", "current_user": "當前用户名",
"current_session": "當前會話",
"password": "密碼", "password": "密碼",
"login_error": "登錄失败", "login_error": "登錄失败",
"run": "執行", "run": "執行",

14
main.go
View File

@ -3,9 +3,11 @@ package main
import ( import (
"flag" "flag"
"html/template" "html/template"
"math/rand"
"mime" "mime"
"net/http" "net/http"
"runtime" "runtime"
"strconv"
"time" "time"
"github.com/b3log/wide/conf" "github.com/b3log/wide/conf"
@ -53,7 +55,9 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
httpSession.Save(r, w) httpSession.Save(r, w)
// create a Wide session // 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) username := httpSession.Values["username"].(string)
user := conf.Wide.GetUser(username) user := conf.Wide.GetUser(username)
@ -112,8 +116,14 @@ func startHandler(w http.ResponseWriter, r *http.Request) {
locale := conf.Wide.GetUser(username).Locale locale := conf.Wide.GetUser(username).Locale
userWorkspace := conf.Wide.GetUserWorkspace(username) 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, 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") t, err := template.ParseFiles("views/start.html")

View File

@ -75,9 +75,8 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
sid := r.URL.Query()["sid"][0] sid := r.URL.Query()["sid"][0]
wSession := session.WideSessions.Get(sid) wSession := session.WideSessions.Get(sid)
if nil == wSession {
glog.Errorf("Session [%s] not found", sid)
if nil == wSession {
return return
} }

View File

@ -10,10 +10,8 @@ package session
import ( import (
"encoding/json" "encoding/json"
"math/rand"
"net/http" "net/http"
"os" "os"
"strconv"
"sync" "sync"
"time" "time"
@ -97,17 +95,26 @@ func WSHandler(w http.ResponseWriter, r *http.Request) {
sid := r.URL.Query()["sid"][0] sid := r.URL.Query()["sid"][0]
wSession := WideSessions.Get(sid) wSession := WideSessions.Get(sid)
if nil == wSession { if nil == wSession {
glog.Errorf("Session [%s] not found", sid) httpSession, _ := HTTPSession.Get(r, "wide-session")
if httpSession.IsNew {
return 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) conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()} wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}
SessionWS[sid] = &wsChan 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) wsChan.Conn.WriteJSON(&ret)
glog.V(4).Infof("Open a new [Session Channel] with session [%s], %d", sid, len(SessionWS)) 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. // 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() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()
rand.Seed(time.Now().UnixNano())
id := strconv.Itoa(rand.Int())
now := time.Now() now := time.Now()
// create user event queue // create user event queuselect
userEventQueue := event.UserEventQueues.New(id) userEventQueue := event.UserEventQueues.New(sid)
ret := &WideSession{ ret := &WideSession{
Id: id, Id: sid,
Username: httpSession.Values["username"].(string), Username: httpSession.Values["username"].(string),
HTTPSession: httpSession, HTTPSession: httpSession,
EventQueue: userEventQueue, EventQueue: userEventQueue,

View File

@ -3,10 +3,12 @@ package shell
import ( import (
"html/template" "html/template"
"math/rand"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"runtime" "runtime"
"strconv"
"strings" "strings"
"time" "time"
@ -37,7 +39,9 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
httpSession.Save(r, w) httpSession.Save(r, w)
// create a wide session // 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) username := httpSession.Values["username"].(string)
locale := conf.Wide.GetUser(username).Locale locale := conf.Wide.GetUser(username).Locale

View File

@ -180,7 +180,7 @@ var editors = {
title: '<span title="' + config.label.start_page + '">' + config.label.start_page + '</span>', title: '<span title="' + config.label.start_page + '">' + config.label.start_page + '</span>',
content: '<div id="startPage"></div>', content: '<div id="startPage"></div>',
after: function () { after: function () {
$("#startPage").load('/start'); $("#startPage").load('/start?sid=' + config.wideSessionId);
$.ajax({ $.ajax({
url: "http://symphony.b3log.org/apis/articles?tags=wide,golang&p=1&size=30", url: "http://symphony.b3log.org/apis/articles?tags=wide,golang&p=1&size=30",
type: "GET", type: "GET",

View File

@ -30,7 +30,6 @@ var notification = {
notificationWS.onclose = function (e) { notificationWS.onclose = function (e) {
console.log('[notification onclose] disconnected (' + e.code + ')'); console.log('[notification onclose] disconnected (' + e.code + ')');
delete notificationWS;
}; };
notificationWS.onerror = function (e) { notificationWS.onerror = function (e) {

View File

@ -83,16 +83,56 @@ var session = {
sessionWS.onopen = function () { sessionWS.onopen = function () {
console.log('[session onopen] connected'); 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 += '<tr><td class="severity">' + data.severity
+ '</td><td class="message">' + data.message
+ '</td><td class="type">' + data.type + '</td></tr>';
$notification.append(notificationHTML);
}; };
sessionWS.onmessage = function (e) { sessionWS.onmessage = function (e) {
console.log('[session onmessage]' + e.data); console.log('[session onmessage]' + e.data);
var data = JSON.parse(e.data);
}; };
sessionWS.onclose = function (e) { sessionWS.onclose = function (e) {
console.log('[session onclose] disconnected (' + e.code + ')'); 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 += '<tr><td class="severity">' + data.severity
+ '</td><td class="message">' + data.message
+ '</td><td class="type">' + data.type + '</td></tr>';
$notification.append(notificationHTML);
$(".notification-count").show();
}; };
sessionWS.onerror = function (e) { sessionWS.onerror = function (e) {
console.log('[session onerror] ' + JSON.parse(e)); console.log('[session onerror] ' + JSON.parse(e));

View File

@ -14,7 +14,6 @@ var shell = {
}; };
shell.shellWS.onclose = function (e) { shell.shellWS.onclose = function (e) {
console.log('[shell onclose] disconnected (' + e.code + ')'); console.log('[shell onclose] disconnected (' + e.code + ')');
delete shell.shellWS;
}; };
shell.shellWS.onerror = function (e) { shell.shellWS.onerror = function (e) {
console.log('[shell onerror] ' + e); console.log('[shell onerror] ' + e);

View File

@ -387,7 +387,6 @@ var wide = {
}; };
outputWS.onclose = function (e) { outputWS.onclose = function (e) {
console.log('[output onclose] disconnected (' + e.code + ')'); console.log('[output onclose] disconnected (' + e.code + ')');
delete outputWS;
}; };
outputWS.onerror = function (e) { outputWS.onerror = function (e) {
console.log('[output onerror] ' + e); console.log('[output onerror] ' + e);

View File

@ -5,6 +5,10 @@
<label>{{.i18n.current_user}}{{.i18n.colon}}</label> <label>{{.i18n.current_user}}{{.i18n.colon}}</label>
{{.username}} {{.username}}
</li> </li>
<li>
<label>{{.i18n.current_session}}{{.i18n.colon}}</label>
{{.session.Id}}
</li>
<li class="border workspace"> <li class="border workspace">
<label>{{.i18n.workspace}}{{.i18n.colon}}</label> <label>{{.i18n.workspace}}{{.i18n.colon}}</label>
<span>{{.workspace}}</span> <span>{{.workspace}}</span>