This commit is contained in:
Liang Ding 2014-09-17 09:41:22 +08:00
parent 004c0a67a7
commit f7cc9ac140
3 changed files with 51 additions and 13 deletions

View File

@ -40,7 +40,7 @@ func init() {
// Wide 首页. // Wide 首页.
func indexHandler(w http.ResponseWriter, r *http.Request) { func indexHandler(w http.ResponseWriter, r *http.Request) {
// 创建一个 Wide 会话 // 创建一个 Wide 会话
wideSession := user.NewSession() wideSession := user.WideSessions.New()
i18n.Load() i18n.Load()
@ -54,7 +54,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
name := conf.Wide.Users[0].Name name := conf.Wide.Users[0].Name
httpSession.Values["username"] = name httpSession.Values["username"] = name
httpSession.Values["id"] = strconv.Itoa(rand.Int()) httpSessionId := strconv.Itoa(rand.Int())
httpSession.Values["id"] = httpSessionId
// 一天过期 // 一天过期
httpSession.Options.MaxAge = 60 * 60 * 24 httpSession.Options.MaxAge = 60 * 60 * 24
@ -63,6 +64,9 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
httpSession.Save(r, w) httpSession.Save(r, w)
// Wide 会话关联 HTTP 会话
wideSession.HTTPSessionId = httpSession.Values["id"].(string)
t, err := template.ParseFiles("view/index.html") t, err := template.ParseFiles("view/index.html")
if nil != err { if nil != err {

View File

@ -5,6 +5,7 @@ import (
"strconv" "strconv"
"time" "time"
"github.com/golang/glog"
"github.com/gorilla/sessions" "github.com/gorilla/sessions"
) )
@ -18,22 +19,54 @@ var Session = sessions.NewCookieStore([]byte("BEYOND"))
// Wide 会话,对应一个浏览器 tab. // Wide 会话,对应一个浏览器 tab.
type WideSession struct { type WideSession struct {
Id string // 唯一标识 Id string // 唯一标识
HTTPSessionId string // HTTP 会话 id
State int // 状态 State int // 状态
Created time.Time // 创建时间 Created time.Time // 创建时间
Updated time.Time // 最近一次使用时间 Updated time.Time // 最近一次使用时间
} }
type Sessions []*WideSession
// 所有 Wide 会话集.
var WideSessions Sessions
// 创建一个 Wide 会话. // 创建一个 Wide 会话.
func NewSession() *WideSession { func (sessions *Sessions) New() *WideSession {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
id := strconv.Itoa(rand.Int()) id := strconv.Itoa(rand.Int())
now := time.Now() now := time.Now()
return &WideSession{ ret := &WideSession{
Id: id, Id: id,
State: SessionStateActive, State: SessionStateActive,
Created: now, Created: now,
Updated: now, Updated: now,
} }
*sessions = append(*sessions, ret)
return ret
}
// 移除 Wide 会话.
func (sessions *Sessions) Remove(sid string) {
for i, s := range *sessions {
if s.Id == sid {
*sessions = append((*sessions)[:i], (*sessions)[i+1:]...)
glog.V(3).Infof("Has [%d] wide sessions currently", len(*sessions))
}
}
}
// 移除 HTTP 会话关联的所有 Wide 会话.
func (sessions *Sessions) RemoveByHTTPSid(httpSessionId string) {
for i, s := range *sessions {
if s.HTTPSessionId == httpSessionId {
*sessions = append((*sessions)[:i], (*sessions)[i+1:]...)
glog.V(3).Infof("Has [%d] wide sessions currently", len(*sessions))
}
}
} }

View File

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>{{.i18n.wide}} - {{.session.Id}}</title> <title>{{.i18n.wide}}</title>
<link rel="stylesheet" href="{{.conf.StaticServer}}/static/js/lib/codemirror-4.5/codemirror.css"> <link rel="stylesheet" href="{{.conf.StaticServer}}/static/js/lib/codemirror-4.5/codemirror.css">
<link rel="stylesheet" href="{{.conf.StaticServer}}/static/js/lib/codemirror-4.5/addon/hint/show-hint.css"> <link rel="stylesheet" href="{{.conf.StaticServer}}/static/js/lib/codemirror-4.5/addon/hint/show-hint.css">
<link rel="stylesheet" href="{{.conf.StaticServer}}/static/js/lib/codemirror-4.5/addon/lint/lint.css"> <link rel="stylesheet" href="{{.conf.StaticServer}}/static/js/lib/codemirror-4.5/addon/lint/lint.css">
@ -167,10 +167,11 @@
<script type="text/javascript"> <script type="text/javascript">
var config = { var config = {
channel: { channel: {
editor: '{{.conf.EditorChannel}}', editor: {{.conf.EditorChannel}},
shell: '{{.conf.ShellChannel}}', shell: {{.conf.ShellChannel}},
output: '{{.conf.OutputChannel}}' output: {{.conf.OutputChannel}}
} },
wideSessionId: {{.session.Id}}
};</script> };</script>
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script> <script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/reconnecting-websocket.js"></script> <script type="text/javascript" src="{{.conf.StaticServer}}/static/js/lib/reconnecting-websocket.js"></script>