新增聊天功能
This commit is contained in:
@@ -2,17 +2,21 @@ package service
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"fyne.io/fyne/v2/data/binding"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"work_cation/cfg"
|
||||
"work_cation/global"
|
||||
"work_cation/models"
|
||||
"work_cation/repo"
|
||||
)
|
||||
|
||||
type ClientService struct {
|
||||
@@ -110,6 +114,33 @@ func (c *ClientService) Download(online *models.Online, uuid string, progress bi
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *ClientService) Chat(online *models.Online, text string) (string, error) {
|
||||
var user = repo.User.GetUserInfo(global.DB)
|
||||
ctx, carnal := context.WithTimeout(context.TODO(), time.Millisecond*500)
|
||||
defer carnal()
|
||||
b, err := json.Marshal(gin.H{"text": text, "uuid": user.ID})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
res, err := http.NewRequestWithContext(ctx, "POST", online.Url("/chat"), bytes.NewBuffer(b))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
res.Header.Set("Content-Type", "application/json")
|
||||
res.Header.Set("User-ID", user.ID)
|
||||
resp, err := http.DefaultClient.Do(res)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := io.ReadAll(resp.Body)
|
||||
var msg = make(map[string]any)
|
||||
if err = json.Unmarshal(data, &msg); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return msg["message"].(string), err
|
||||
}
|
||||
|
||||
type Rr struct {
|
||||
file *os.File
|
||||
tot int64
|
||||
|
||||
+30
-7
@@ -10,8 +10,10 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
"work_cation/cfg"
|
||||
"work_cation/global"
|
||||
"work_cation/models"
|
||||
"work_cation/repo"
|
||||
)
|
||||
|
||||
@@ -36,19 +38,15 @@ func (s *serverService) Online() error {
|
||||
if err := Zeroconf.Register(user); err != nil {
|
||||
return err
|
||||
}
|
||||
// 启动服务
|
||||
if err := s.StartListenServer(); err != nil {
|
||||
return err
|
||||
}
|
||||
// 修改状态
|
||||
s.Status = StatusOnline
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *serverService) StatusOffline() error {
|
||||
if err := s.server.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
//if err := s.server.Close(); err != nil {
|
||||
// return err
|
||||
//}
|
||||
// 关闭服务发现
|
||||
Zeroconf.Close()
|
||||
// 修改状态
|
||||
@@ -87,6 +85,31 @@ func (s *serverService) StartListenServer() error {
|
||||
}
|
||||
})
|
||||
|
||||
router.POST("/chat", func(c *gin.Context) {
|
||||
uuid := c.GetHeader("User-ID")
|
||||
user := repo.UserFollow.GetUser(global.DB, uuid)
|
||||
if user.Ip != c.ClientIP() {
|
||||
c.JSON(200, gin.H{"message": "对方未关注你"})
|
||||
return
|
||||
}
|
||||
var msg = make(map[string]interface{})
|
||||
if err := c.ShouldBind(&msg); err != nil {
|
||||
c.JSON(200, gin.H{"message": "输入异常"})
|
||||
return
|
||||
}
|
||||
message := &models.Message{
|
||||
Cmd: "chat",
|
||||
User: *user,
|
||||
ChatMessage: models.ChatMessage{
|
||||
User: user.Users,
|
||||
Time: time.Now(),
|
||||
Text: msg["text"].(string),
|
||||
},
|
||||
}
|
||||
global.Send.SendChan <- message
|
||||
c.JSON(200, gin.H{"message": "ok"})
|
||||
})
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: cfg.T.ServerAddr,
|
||||
Handler: router,
|
||||
|
||||
@@ -42,6 +42,15 @@ func (s *zeroconfService) FindService() ([]models.Online, error) {
|
||||
return s.onlines, nil
|
||||
}
|
||||
|
||||
func (s *zeroconfService) GetOnline(id string) *models.Online {
|
||||
for _, online := range s.onlines {
|
||||
if online.ID == id {
|
||||
return &online
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *zeroconfService) StartFindService() error {
|
||||
resolver, err := zeroconf.NewResolver(nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user