This commit is contained in:
xxj
2025-07-18 11:42:44 +08:00
parent 14480c8246
commit ce9ce4f572
4 changed files with 47 additions and 27 deletions

View File

@@ -35,26 +35,43 @@ func NewSocketUpgrader(sessionId string, w http.ResponseWriter, r *http.Request)
return &Socket{ return &Socket{
SessionId: sessionId, SessionId: sessionId,
conn: conn, conn: conn,
d: time.Second * 30,
}, nil }, nil
} }
type Socket struct { type Socket struct {
sync.Mutex
SessionId string SessionId string
conn *gws.Conn conn *gws.Conn
sync.Mutex d time.Duration
} }
func (s *Socket) ID() string { func (s *Socket) ID() string {
return s.SessionId return s.SessionId
} }
func (s *Socket) SetTimeOut(d time.Duration) {
s.d = d
}
func (s *Socket) WriteMessage(byteMessage []byte) error { func (s *Socket) WriteMessage(byteMessage []byte) error {
s.Mutex.Lock() s.Mutex.Lock()
defer s.Mutex.Unlock() defer s.Mutex.Unlock()
return s.conn.WriteMessage(gws.BinaryMessage, byteMessage) return s.conn.WriteMessage(gws.BinaryMessage, byteMessage)
} }
func (s *Socket) WriteText(msg string) error {
s.Mutex.Lock()
defer s.Mutex.Unlock()
return s.conn.WriteMessage(gws.TextMessage, []byte(msg))
}
func (s *Socket) ReadMessage() (messageType int, p []byte, err error) { func (s *Socket) ReadMessage() (messageType int, p []byte, err error) {
// 设置读取超时示例5秒
if err = s.conn.SetReadDeadline(time.Now().Add(s.d)); err != nil {
// 处理错误
return
}
return s.conn.ReadMessage() return s.conn.ReadMessage()
} }

View File

@@ -206,3 +206,20 @@ func isPunct(r rune) bool {
return false return false
} }
func StringToInt(str string) int {
i, _ := strconv.Atoi(str)
return i
}
func StringToInt64(str string) int64 {
int64, _ := strconv.ParseInt(str, 10, 64)
return int64
}
func Abs(in int) int {
if in < 0 {
return -in
}
return in
}

View File

@@ -2,7 +2,6 @@ package tools
import ( import (
"bufio" "bufio"
"fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
@@ -17,7 +16,6 @@ import (
func CheckFileIsExist(filename string) bool { func CheckFileIsExist(filename string) bool {
var exist = true var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) { if _, err := os.Stat(filename); os.IsNotExist(err) {
mylog.Debug(err)
exist = false exist = false
} }
return exist return exist
@@ -129,26 +127,15 @@ func WriteFileEx(fname string, src []byte, isClear bool) bool {
// ReadFileEx 读取文件 // ReadFileEx 读取文件
func ReadFileEx(fname string) []byte { func ReadFileEx(fname string) []byte {
f, err := os.OpenFile(fname, os.O_RDONLY, 0666) if !CheckFileIsExist(fname) {
if err != nil {
return []byte{}
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
size := stat.Size()
data := make([]byte, size)
_, err = f.Read(data)
if err != nil {
return []byte{} return []byte{}
} }
data, err := os.ReadFile(fname) // 使用 os.ReadFile (Go 1.16+)
if err != nil {
mylog.Error(err)
return []byte{}
}
return data return data
} }

View File

@@ -3,10 +3,9 @@ package tools
import ( import (
// "encoding/json" // "encoding/json"
"encoding/json"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/bytedance/sonic"
) )
// JSONToForm tag json str to form // JSONToForm tag json str to form
@@ -18,7 +17,7 @@ func JSONToForm(r *http.Request) {
if len(value[0]) == 0 { if len(value[0]) == 0 {
delete(r.Form, key) delete(r.Form, key)
var m map[string]string var m map[string]string
if err := sonic.Unmarshal([]byte(key), &m); err == nil { if err := json.Unmarshal([]byte(key), &m); err == nil {
for k, v := range m { for k, v := range m {
r.Form[k] = []string{v} r.Form[k] = []string{v}
} }
@@ -31,7 +30,7 @@ func JSONToForm(r *http.Request) {
bodyStr := string(body) bodyStr := string(body)
if len(bodyStr) > 0 { if len(bodyStr) > 0 {
var m map[string]string var m map[string]string
if err := sonic.Unmarshal(body, &m); err == nil { if err := json.Unmarshal(body, &m); err == nil {
for k, v := range m { for k, v := range m {
r.Form[k] = []string{v} r.Form[k] = []string{v}
} }
@@ -56,9 +55,9 @@ func JSONToForm(r *http.Request) {
func GetJSONStr(obj interface{}, isFormat bool) string { func GetJSONStr(obj interface{}, isFormat bool) string {
var b []byte var b []byte
if isFormat { if isFormat {
b, _ = sonic.MarshalIndent(obj, "", " ") b, _ = json.MarshalIndent(obj, "", " ")
} else { } else {
b, _ = sonic.Marshal(obj) b, _ = json.Marshal(obj)
} }
return string(b) return string(b)
} }
@@ -70,7 +69,7 @@ func JSONDecode(obj interface{}) string {
// GetJSONObj string convert to obj // GetJSONObj string convert to obj
func GetJSONObj(str string, out interface{}) error { func GetJSONObj(str string, out interface{}) error {
return sonic.Unmarshal([]byte(str), out) return json.Unmarshal([]byte(str), out)
} }
// JSONEncode string convert to obj // JSONEncode string convert to obj