-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
notice.go
126 lines (116 loc) · 3.68 KB
/
notice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package user
import (
"encoding/json"
"github.com/admpub/log"
"github.com/admpub/nging/v5/application/dbschema"
"github.com/admpub/nging/v5/application/handler"
"github.com/admpub/nging/v5/application/library/notice"
"github.com/admpub/websocket"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/webx-top/echo/defaults"
)
func init() {
notice.OnOpen(func(user string) {
ctx := defaults.NewMockContext()
userM := dbschema.NewNgingUser(ctx)
err := userM.UpdateField(nil, `online`, `Y`, `username`, user)
if err != nil {
log.Error(err)
}
})
notice.OnClose(func(user string) {
ctx := defaults.NewMockContext()
userM := dbschema.NewNgingUser(ctx)
err := userM.UpdateField(nil, `online`, `N`, `username`, user)
if err != nil {
log.Error(err)
}
})
}
func Notice(c *websocket.Conn, ctx echo.Context) error {
user := handler.User(ctx)
if user == nil {
return ctx.NewError(code.Unauthenticated, `登录信息获取失败,请重新登录`)
}
oUser, clientID := notice.OpenClient(user.Username)
defer notice.CloseClient(user.Username, clientID)
//push(writer)
go func(user *dbschema.NgingUser, clientID string) {
message, err := json.Marshal(notice.NewMessage().SetMode(`-`).SetType(`clientID`).SetClientID(clientID))
if err != nil {
handler.WebSocketLogger.Error(`Push error: `, err.Error())
return
}
handler.WebSocketLogger.Debug(`Push message: `, string(message))
if err = c.WriteMessage(websocket.TextMessage, message); err != nil {
if websocket.IsCloseError(err, websocket.CloseGoingAway) {
handler.WebSocketLogger.Debug(`Push error: `, err.Error())
} else {
handler.WebSocketLogger.Error(`Push error: `, err.Error())
}
return
}
msgChan := oUser.Recv(clientID)
if msgChan == nil {
return
}
for {
//message := []byte(echo.Dump(notice.NewMessageWithValue(`type`, `title`, `content:`+time.Now().Format(time.RFC1123)), false))
//time.Sleep(time.Second)
message := <-msgChan
if message == nil {
return
}
msgBytes, err := json.Marshal(message)
if err != nil {
handler.WebSocketLogger.Error(`Push error (json.Marshal): `, err.Error())
return
}
handler.WebSocketLogger.Debug(`Push message: `, string(msgBytes))
if err = c.WriteMessage(websocket.TextMessage, msgBytes); err != nil {
if websocket.IsCloseError(err, websocket.CloseGoingAway) {
handler.WebSocketLogger.Debug(`Push error: `, err.Error())
} else {
handler.WebSocketLogger.Error(`Push error: `, err.Error())
}
return
}
}
}(user, clientID)
//echo
var execute = func(conn *websocket.Conn) error {
for {
mt, message, err := conn.ReadMessage()
if err != nil {
return err
}
if err = conn.WriteMessage(mt, message); err != nil {
return err
}
}
}
err := execute(c)
if err != nil {
if websocket.IsCloseError(err, websocket.CloseGoingAway) {
handler.WebSocketLogger.Debug(err.Error())
} else {
handler.WebSocketLogger.Error(err.Error())
}
}
return nil
}