From 67edf948bbfdeb54f60360b6a7fc59b203a9ec14 Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Wed, 10 Jun 2020 14:23:20 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=A1=B5=E5=92=A8=E8=AF=A2?= =?UTF-8?q?=E5=BC=B9=E7=AA=97=E5=92=8C=E5=AE=9A=E6=97=B6=E5=90=8E=E5=8F=B0?= =?UTF-8?q?=E5=88=A0=E6=9D=83=E9=99=90=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/chat.go | 6 +- controller/timer.go | 37 +++++++ controller/timer_linux.go | 35 +++++++ server.go | 4 + static/html/chat_page.html | 203 +++++++++++++++++++++++++++++++++++++ static/html/login.html | 171 ++++--------------------------- tools/snowflake.go | 81 +++++++++++++++ 7 files changed, 384 insertions(+), 153 deletions(-) create mode 100644 controller/timer.go create mode 100644 controller/timer_linux.go create mode 100644 static/html/chat_page.html create mode 100644 tools/snowflake.go diff --git a/controller/chat.go b/controller/chat.go index 7c2f2f8..8c74532 100644 --- a/controller/chat.go +++ b/controller/chat.go @@ -15,7 +15,11 @@ func ActionChatMain(w http.ResponseWriter, r *http.Request) { render := tmpl.NewRender(w) render.Display("chat_main", nil) } - +//聊天客户端界面 +func ActionChatPage(w http.ResponseWriter, r *http.Request){ + render := tmpl.NewRender(w) + render.Display("chat_page", nil) +} //获取在线用户 func ChatUsers(w http.ResponseWriter, r *http.Request) { w.Header().Set("content-type", "text/json;charset=utf-8;") diff --git a/controller/timer.go b/controller/timer.go new file mode 100644 index 0000000..92cbd4a --- /dev/null +++ b/controller/timer.go @@ -0,0 +1,37 @@ +//+build !linux + +package controller + +import ( + "github.com/taoshihan1991/imaptool/config" + "os" + "path/filepath" + "runtime" + "syscall" + "time" +) +var osType = runtime.GOOS +const expireTime=30*60 +//检测权限文件是否过期,超过30分钟删除掉 +func TimerSessFile(){ + go func() { + for { + time.Sleep(time.Second*10) + files,_:=filepath.Glob(config.Dir+"sess_*") + for _,file:=range files{ + fileInfo, _ := os.Stat(file) + var createTime int64 + now:=time.Now().Unix() + if osType == "windows" { + wFileSys := fileInfo.Sys().(*syscall.Win32FileAttributeData) + tNanSeconds := wFileSys.CreationTime.Nanoseconds() /// 返回的是纳秒 + createTime = tNanSeconds/1e9 ///秒 + } + diffTime:=now-createTime + if diffTime>expireTime{ + os.Remove(file) + } + } + } + }() +} diff --git a/controller/timer_linux.go b/controller/timer_linux.go new file mode 100644 index 0000000..ca7a86e --- /dev/null +++ b/controller/timer_linux.go @@ -0,0 +1,35 @@ +package controller + +import ( + "github.com/taoshihan1991/imaptool/config" + "log" + "os" + "path/filepath" + "runtime" + "time" +) +var osType = runtime.GOOS +const expireTime=30*60 +//检测权限文件是否过期,超过30分钟删除掉 +func TimerSessFile(){ + go func() { + for { + time.Sleep(time.Second*10) + files,_:=filepath.Glob(config.Dir+"sess_*") + for _,file:=range files{ + fileInfo, _ := os.Stat(file) + var createTime int64 + now:=time.Now().Unix() + if osType == "linux" { + stat_t := fileInfo.Sys().(*syscall.Stat_t) + createTime = int64(stat_t.Ctim.Sec) + } + diffTime:=now-createTime + if diffTime>expireTime{ + os.Remove(file) + } + } + log.Println(files) + } + }() +} diff --git a/server.go b/server.go index 4a6aa18..faddd23 100644 --- a/server.go +++ b/server.go @@ -41,10 +41,14 @@ func main() { mux.HandleFunc("/chat_main", controller.ActionChatMain) //新邮件提醒服务 mux.HandleFunc("/push_mail", controller.PushMailServer) + //聊天界面 + mux.HandleFunc("/chat_page",controller.ActionChatPage) //聊天服务 mux.Handle("/chat_server",websocket.Handler(controller.ChatServer)) //获取在线用户 mux.HandleFunc("/chat_users", controller.ChatUsers) + //后台任务 + controller.TimerSessFile() //监听端口 //http.ListenAndServe(":8080", nil) //var myHandler http.Handler diff --git a/static/html/chat_page.html b/static/html/chat_page.html new file mode 100644 index 0000000..04fdc16 --- /dev/null +++ b/static/html/chat_page.html @@ -0,0 +1,203 @@ + +
+ + + + +© 2020
-