Browse Source

change chat

master
godo 9 months ago
parent
commit
02bfa0d445
  1. 29
      frontend/src/stores/localchat.ts
  2. 14
      godo/localchat/udp.go
  3. 5
      godo/sys/msg.go

29
frontend/src/stores/localchat.ts

@ -27,8 +27,19 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
const chatTargetId = ref(0) const chatTargetId = ref(0)
const chatTargetIp = ref("") const chatTargetIp = ref("")
const showAddUser = ref(false) const showAddUser = ref(false)
const handlerMessage = (message : any) => { const handlerMessage = (data : any) => {
console.log(message) //console.log(data)
if(data.onlines && data.onlines.length > 0){
setUserList(data.onlines);
}
if(data.messages){
for(let i = 0; i < data.messages.length; i++){
const msg = data.messages[i];
if(msg.type == 'text'){
addText(msg);
}
}
}
} }
const handleSelect = (key: number) => { const handleSelect = (key: number) => {
navId.value = key; navId.value = key;
@ -185,8 +196,6 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
if (data.length < 1) { if (data.length < 1) {
return return
} }
hostInfo.value = data[0]
data.shift()
const existingIps = new Set(userList.value.map((d : any) => d.ip)); const existingIps = new Set(userList.value.map((d : any) => d.ip));
const updates: any[] = []; const updates: any[] = [];
const newEntries: any[] = []; const newEntries: any[] = [];
@ -284,13 +293,13 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
} }
} }
const getTargetUser = async (data: any) => { const getTargetUser = async (data: any) => {
let targetUser: any = userList.value.find((d: any) => d.ip === data.senderInfo.ip) let targetUser: any = userList.value.find((d: any) => d.ip === data.ip)
if (!targetUser) { if (!targetUser) {
targetUser = { targetUser = {
isOnline: true, isOnline: true,
ip: data.senderInfo.ip, ip: data.ip,
hostname: data.senderInfo.hostname, hostname: data.hostname,
username: data.senderInfo.hostname, username: data.hostname,
createdAt: Date.now(), createdAt: Date.now(),
updatedAt: Date.now() updatedAt: Date.now()
} }
@ -306,8 +315,7 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
type: 'text', type: 'text',
targetId: targetUser.id, targetId: targetUser.id,
targetIp: targetUser.ip, targetIp: targetUser.ip,
content: data.content, content: data.message,
reciperInfo: data.senderInfo,
createdAt: Date.now(), createdAt: Date.now(),
isMe: false, isMe: false,
isRead: false, isRead: false,
@ -334,7 +342,6 @@ export const useLocalChatStore = defineStore('localChatStore', () => {
targetId: chatTargetId.value, targetId: chatTargetId.value,
targetIp: chatTargetIp.value, targetIp: chatTargetIp.value,
content: sendInfo.value.trim(), content: sendInfo.value.trim(),
senderInfo: toRaw(hostInfo.value),
createdAt: Date.now(), createdAt: Date.now(),
isMe: true, isMe: true,
isRead: false, isRead: false,

14
godo/localchat/udp.go

@ -24,6 +24,10 @@ type UdpAddress struct {
type Messages struct { type Messages struct {
Messages []UdpMessage `json:"messages"` Messages []UdpMessage `json:"messages"`
} }
type UserMessage struct {
Messages map[string]*Messages `json:"messages"`
Onlines []UdpAddress `json:"onlines"`
}
var OnlineUsers []UdpAddress var OnlineUsers []UdpAddress
var UserMessages = make(map[string]*Messages) // 使用指针类型 var UserMessages = make(map[string]*Messages) // 使用指针类型
@ -122,11 +126,11 @@ func ClearAllUserMessages() {
UserMessages[ip] = msg // 更新映射中的值 UserMessages[ip] = msg // 更新映射中的值
} }
} }
func GetUserMessages() map[string]*Messages { func GetMessages() UserMessage {
return UserMessages return UserMessage{
} Messages: UserMessages,
func GetOnlineUsers() []UdpAddress { Onlines: OnlineUsers,
return OnlineUsers }
} }
func GetBroadcastAddr() string { func GetBroadcastAddr() string {
return libs.GetUdpAddr() return libs.GetUdpAddr()

5
godo/sys/msg.go

@ -61,12 +61,13 @@ func HandleSystemEvents(w http.ResponseWriter, r *http.Request) {
if r.Context().Err() != nil { if r.Context().Err() != nil {
return return
} }
onlineUsers := localchat.GetOnlineUsers() userMessages := localchat.GetMessages()
msg := Message{ msg := Message{
Type: "localchat", Type: "localchat",
Data: onlineUsers, Data: userMessages,
} }
Broadcast(msg) Broadcast(msg)
localchat.ClearAllUserMessages()
} }
} }

Loading…
Cancel
Save