pull/30/head
taoshihan1991 5 years ago
parent 1563f1dc15
commit ce37a9f432

@ -0,0 +1,29 @@
package controller
import(
"github.com/gorilla/websocket"
"log"
"net/http"
)
var upgrader = websocket.Upgrader{}
//推送新邮件到达
func PushMailServer(w http.ResponseWriter, r *http.Request){
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, []byte("{\"title\":\"测试\"}"))
if err != nil {
log.Println("write:", err)
break
}
}
}

@ -10,6 +10,7 @@ require (
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21
github.com/emersion/go-smtp v0.13.0
github.com/gobuffalo/packr v1.30.1
github.com/gorilla/websocket v1.4.2
golang.org/x/net v0.0.0-20200513185701-a91f0712d120
golang.org/x/text v0.3.2
)

@ -31,6 +31,8 @@ github.com/gobuffalo/packd v0.3.0/go.mod h1:zC7QkmNkYVGKPw4tHpBQ+ml7W/3tIebgeo1b
github.com/gobuffalo/packr v1.30.1 h1:hu1fuVR3fXEZR7rXNW3h8rqSML8EVAf6KNm0NKO/wKg=
github.com/gobuffalo/packr v1.30.1/go.mod h1:ljMyFO2EcrnzsHsN99cvbq055Y9OhRrIaviy289eRuk=
github.com/gobuffalo/packr/v2 v2.5.1/go.mod h1:8f9c96ITobJlPzI44jj+4tHnEKNt0xXWSVlXRN9X1Iw=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=

@ -41,6 +41,8 @@ func main() {
mux.HandleFunc("/send", controller.FolderSend)
//聊天界面
mux.HandleFunc("/chat_main", controller.ActionMain)
//推送新邮件
mux.HandleFunc("/push_mail", controller.PushMailServer)
//监听端口
//http.ListenAndServe(":8080", nil)
//var myHandler http.Handler

@ -51,3 +51,4 @@
</style>
</head>
<body class="text-center">

@ -1,5 +1,4 @@
{{.Header}}
<body class="text-center">
<div id="app" style="width:100%">
<template>
<el-container v-loading.fullscreen.lock="fullscreenLoading">

@ -1,5 +1,4 @@
{{.Header}}
<body class="text-center">
<div id="app" style="width:100%">
<template>
<el-container v-loading.fullscreen.lock="fullscreenLoading">

@ -1,5 +1,57 @@
{{.Header}}
<body>
<html lang="cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="陶士涵">
<title>GO-IMAP网页版邮箱imap工具</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/theme-chalk/index.css">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/element-ui@2.13.1/lib/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
<style>
html,
body {
height: 100%;
padding: 0;
margin: 0;
}
body {
overflow: hidden;
background-color: #f5f5f5;
}
.el-container{
margin-top:10px;
height: 100%;
}
.el-aside{
height: 100%;
background: #fff;
border: solid 1px #e6e6e6;
}
.el-aside .el-menu{
border-right: none;
}
.mainLogo{
font-size: 20px;
font-weight: bold;
}
.mainMain{
background: #fff;
margin-left: 10px;
margin-bottom: 60px;
}
.mainIframe{
width: 100%;
height: 100%;
}
.el-card__body{
cursor: pointer;
}
</style>
</head>
<body class="text-center">
<div id="app">
<template>
{{.Nav}}
@ -29,4 +81,47 @@
})
</script>
<script>
new Vue({
delimiters:["<{","}>"],
data: {
websock: null,
},
created() {
this.initWebSocket();
},
destroyed() {
this.websock.close() //离开路由之后断开websocket连接
},
methods: {
initWebSocket(){ //初始化weosocket
const wsuri = "ws://127.0.0.1:8080/push_mail";
this.websock = new WebSocket(wsuri);
this.websock.onmessage = this.websocketonmessage;
this.websock.onopen = this.websocketonopen;
this.websock.onerror = this.websocketonerror;
this.websock.onclose = this.websocketclose;
},
websocketonopen(){ //连接建立之后执行send方法发送数据
let actions = {"test":"12345"};
let _this=this;
setInterval(function(){
_this.websocketsend(JSON.stringify(actions));
},10000);
},
websocketonerror(){//连接建立失败重连
this.initWebSocket();
},
websocketonmessage(e){ //数据接收
const redata = JSON.parse(e.data);
},
websocketsend(Data){//数据发送
this.websock.send(Data);
},
websocketclose(e){ //关闭
console.log('断开连接',e);
},
},
});
</script>
</html>

@ -1,5 +1,4 @@
{{.Header}}
<body class="text-center">
<div id="app" style="width:100%">
<template>
<el-container v-loading.fullscreen.lock="fullscreenLoading">

@ -1,5 +1,4 @@
{{.Header}}
<body class="text-center">
<div id="app" style="width:100%">
<template>
<el-container v-loading.fullscreen.lock="fullscreenLoading">

Loading…
Cancel
Save