Browse Source

change online

master
godo 9 months ago
parent
commit
8a4cc9c87c
  1. 11
      frontend/src/stores/localchat.ts
  2. 5
      godo/cmd/main.go
  3. 2
      godo/libs/chatip.go
  4. 6
      godo/localchat/addr.go
  5. 1
      godo/localchat/check.go
  6. 39
      godo/localchat/server.go

11
frontend/src/stores/localchat.ts

@ -29,8 +29,15 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
const showAddUser = ref(false)
const handlerMessage = (data : any) => {
console.log(data)
if(data.onlines && data.onlines.length > 0){
setUserList(data.onlines);
if(data.onlines){
const ips = []
for(let ip in data.onlines){
const info = data.onlines[ip]
if (info.ip && isValidIP(info.ip)) {
ips.push(info)
}
}
setUserList(ips);
}
if(data.messages){
for(let ip in data.messages){

5
godo/cmd/main.go

@ -80,9 +80,8 @@ func OsStart() {
localchatRouter := router.PathPrefix("/localchat").Subrouter()
localchatRouter.HandleFunc("/message", localchat.HandleMessage).Methods(http.MethodPost)
//localchatRouter.HandleFunc("/file", localchat.HandlerFile).Methods(http.MethodPost)
localchatRouter.HandleFunc("/udpaddr", localchat.HandleAddr).Methods(http.MethodGet)
localchatRouter.HandleFunc("/check", localchat.HandleCheck).Methods(http.MethodGet)
localchatRouter.HandleFunc("/file", localchat.HandlerFile).Methods(http.MethodPost)
localchatRouter.HandleFunc("/setting", localchat.HandleAddr).Methods(http.MethodPost)
// 注册 WebDAV 路由
webdavRouter := router.PathPrefix("/webdav").Subrouter()
webdavRouter.HandleFunc("/read", webdav.HandleReadDir).Methods(http.MethodGet)

2
godo/libs/chatip.go

@ -6,6 +6,7 @@ import (
)
type UserChatIpSetting struct {
CheckTime int `json:"CheckTime"`
First string `json:"First"`
Second string `json:"Second"`
ThirdStart string `json:"ThirdStart"`
@ -16,6 +17,7 @@ type UserChatIpSetting struct {
func GetDefaultChatIpSetting() UserChatIpSetting {
return UserChatIpSetting{
CheckTime: 60,
First: "192",
Second: "168",
ThirdStart: "1",

6
godo/localchat/addr.go

@ -13,12 +13,6 @@ import (
"godo/libs"
)
type UserStatus struct {
Hostname string `json:"hostname"`
IP string `json:"ip"`
Time time.Time `json:"time"`
}
// 发送 UDP 包并忽略响应
func sendUDPPacket(ip string) error {
hostname, err := os.Hostname()

1
godo/localchat/check.go

@ -23,5 +23,6 @@ func HandleAddr(w http.ResponseWriter, r *http.Request) {
return
}
libs.SetConfigByName("ChatIpSetting", ipStr)
go CheckOnline()
libs.SuccessMsg(w, nil, "success")
}

39
godo/localchat/server.go

@ -2,6 +2,7 @@ package localchat
import (
"encoding/json"
"godo/libs"
"log"
"net"
"time"
@ -14,25 +15,31 @@ type UdpMessage struct {
IP string `json:"ip"`
Message any `json:"message"`
}
type Messages struct {
Messages []UdpMessage `json:"messages"`
}
type UserMessage struct {
Messages map[string]*Messages `json:"messages"`
Onlines map[string]UserStatus `json:"onlines"`
Messages map[string][]UdpMessage `json:"messages"`
Onlines map[string]UserStatus `json:"onlines"`
}
type UserStatus struct {
Hostname string `json:"hostname"`
IP string `json:"ip"`
Time time.Time `json:"time"`
}
var OnlineUsers = make(map[string]UserStatus)
var UserMessages = make(map[string]*Messages)
var UserMessages = make(map[string][]UdpMessage)
func init() {
go UdpServer()
go CheckOnlines()
}
func CheckOnlines() {
CheckOnline()
ticker := time.NewTicker(60 * time.Second)
chatIpSetting := libs.GetChatIpSetting()
checkTimeDuration := time.Duration(chatIpSetting.CheckTime) * time.Second
ticker := time.NewTicker(checkTimeDuration)
defer ticker.Stop()
for range ticker.C {
// 检查客户端是否已断开连接
@ -91,21 +98,25 @@ func UdpServer() {
}
}
}
func ClearAllUserMessages() {
for ip, msg := range UserMessages {
msg.Messages = []UdpMessage{} // 清空切片
UserMessages[ip] = msg // 更新映射中的值
}
UserMessages = make(map[string][]UdpMessage)
}
func GetMessages() UserMessage {
return UserMessage{
Messages: UserMessages,
Onlines: OnlineUsers,
}
}
func AddMessage(msg UdpMessage) {
// 检查 UserMessages 中是否已经有这个 IP 地址的消息列表
if _, ok := UserMessages[msg.IP]; !ok {
UserMessages[msg.IP] = &Messages{}
// 如果没有,则创建一个新的消息列表
UserMessages[msg.IP] = []UdpMessage{}
}
UserMessages[msg.IP].Messages = append(UserMessages[msg.IP].Messages, msg)
// 将新消息添加到对应 IP 地址的消息列表中
UserMessages[msg.IP] = append(UserMessages[msg.IP], msg)
}

Loading…
Cancel
Save