diff --git a/doc/zh_CN/dev/index.html b/doc/zh_CN/dev/index.html index e287ad3..daa07b2 100644 --- a/doc/zh_CN/dev/index.html +++ b/doc/zh_CN/dev/index.html @@ -29,14 +29,15 @@ 例如在执行“构建 & 运行”时,Wide 会设置命令 go build 执行的环境变量 $GOPATH 为用户的工作空间,这样编译过程就是在该用户的工作空间中进行,保证其隔离性。

-

Tabs

+

Wide 会话

- 用户可以打开多个编辑器 tabs,但输出窗口只有一个。同时运行多个程序的场景可以通过打开多个浏览器 tabs 达成。 + 在 UI 上存在两种 tab:浏览器 tab;编辑器 tab。 + 用户可以在一个浏览器 tab 中打开多个编辑器 tabs,但输出窗口只有一个。这意味着一个浏览器 tab 只能有一个正在运行的用户代码的程序进程。 + 需要同时运行多个程序进程的场景可以通过打开多个浏览器 tabs 达成。

- 使用编辑器 tabs (而不使用浏览器 tabs)主要是考虑到和文件树的同步显示,这是非常实用的功能。 - 在大多数情况下,一个浏览器 tab 使用 Wide 应该就可以完成开发者的大多数需求,这样的用户体验也更接近桌面 IDE。 + 一个浏览器 tab 对应一个 Wide 会话:只要打开/刷新一个 tab,就会新建一个 Wide 会话。

WebSocket

@@ -48,12 +49,12 @@
  • 通知通道
  • Shell 通道
  • - - 通道和用户会话一对一关联,也就是说如果用户新开浏览器 tab,则老的 tab 的通道就会失效。目前为了简化才这样实现,后续需要改进为每个浏览器 tab 都开不同的通道。 + + 通道和会话一对一关联,也就是说如果用户新开浏览器 tab,则新建通道何其关联。

    事件与通知

    - 每个用户会话都有一个对应的事件队列,当接收到事件时取出该事件并转为通知,再通过通知窗口通道推送给前端。 + 每个会话都有一个对应的事件队列,当接收到事件时取出该事件并转为通知,再通过通知窗口通道推送给前端。 还存在一个全局事件队列,入队的事件将分发到每个用户的事件队列中,以便进行通知广播。

    diff --git a/main.go b/main.go index 0fcee6d..9efef3e 100644 --- a/main.go +++ b/main.go @@ -39,25 +39,29 @@ func init() { // Wide 首页. func indexHandler(w http.ResponseWriter, r *http.Request) { + // 创建一个 Wide 会话 + wideSession := user.NewSession() + i18n.Load() - model := map[string]interface{}{"Wide": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r)} + model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r), + "session": wideSession} - session, _ := user.Session.Get(r, "wide-session") + httpSession, _ := user.Session.Get(r, "wide-session") - if session.IsNew { + if httpSession.IsNew { // TODO: 写死以 admin 作为用户登录 name := conf.Wide.Users[0].Name - session.Values["username"] = name - session.Values["id"] = strconv.Itoa(rand.Int()) + httpSession.Values["username"] = name + httpSession.Values["id"] = strconv.Itoa(rand.Int()) // 一天过期 - session.Options.MaxAge = 60 * 60 * 24 + httpSession.Options.MaxAge = 60 * 60 * 24 - glog.Infof("Created a session [%s] for user [%s]", session.Values["id"].(string), name) + glog.Infof("Created a session [%s] for user [%s]", httpSession.Values["id"].(string), name) } - session.Save(r, w) + httpSession.Save(r, w) t, err := template.ParseFiles("view/index.html") diff --git a/shell/shells.go b/shell/shells.go index d81a56d..6e41820 100644 --- a/shell/shells.go +++ b/shell/shells.go @@ -28,7 +28,7 @@ var shellWS = map[string]*util.WSChannel{} func IndexHandler(w http.ResponseWriter, r *http.Request) { i18n.Load() - model := map[string]interface{}{"Wide": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r)} + model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r)} session, _ := user.Session.Get(r, "wide-session") diff --git a/user/sessions.go b/user/sessions.go index 7090105..162596e 100644 --- a/user/sessions.go +++ b/user/sessions.go @@ -1,8 +1,39 @@ package user import ( + "math/rand" + "strconv" + "time" + "github.com/gorilla/sessions" ) -// 用户会话. +const ( + SessionStateActive = iota // 会话状态:活的 +) + +// 用户 HTTP 会话,用于验证登录. var Session = sessions.NewCookieStore([]byte("BEYOND")) + +// Wide 会话,对应一个浏览器 tab. +type WideSession struct { + Id string // 唯一标识 + State int // 状态 + Created time.Time // 创建时间 + Updated time.Time // 最近一次使用时间 +} + +// 创建一个 Wide 会话. +func NewSession() *WideSession { + rand.Seed(time.Now().UnixNano()) + + id := strconv.Itoa(rand.Int()) + now := time.Now() + + return &WideSession{ + Id: id, + State: SessionStateActive, + Created: now, + Updated: now, + } +} diff --git a/view/index.html b/view/index.html index 0718221..4a70580 100644 --- a/view/index.html +++ b/view/index.html @@ -2,18 +2,18 @@ - {{.i18n.wide}} - - - - - - + {{.i18n.wide}} - {{.session.Id}} + + + + + + - + - - + + @@ -167,52 +167,52 @@ - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - + + + + + + + + + diff --git a/view/shell.html b/view/shell.html index 94352f2..be1a015 100644 --- a/view/shell.html +++ b/view/shell.html @@ -3,7 +3,7 @@ {{.i18n.wide}} - + @@ -15,14 +15,14 @@ - - + + - +