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{
SessionId: sessionId,
conn: conn,
d: time.Second * 30,
}, nil
}
type Socket struct {
sync.Mutex
SessionId string
conn *gws.Conn
sync.Mutex
d time.Duration
}
func (s *Socket) ID() string {
return s.SessionId
}
func (s *Socket) SetTimeOut(d time.Duration) {
s.d = d
}
func (s *Socket) WriteMessage(byteMessage []byte) error {
s.Mutex.Lock()
defer s.Mutex.Unlock()
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) {
// 设置读取超时示例5秒
if err = s.conn.SetReadDeadline(time.Now().Add(s.d)); err != nil {
// 处理错误
return
}
return s.conn.ReadMessage()
}

View File

@@ -206,3 +206,20 @@ func isPunct(r rune) bool {
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 (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
@@ -17,7 +16,6 @@ import (
func CheckFileIsExist(filename string) bool {
var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) {
mylog.Debug(err)
exist = false
}
return exist
@@ -129,26 +127,15 @@ func WriteFileEx(fname string, src []byte, isClear bool) bool {
// ReadFileEx 读取文件
func ReadFileEx(fname string) []byte {
f, err := os.OpenFile(fname, os.O_RDONLY, 0666)
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 {
if !CheckFileIsExist(fname) {
return []byte{}
}
data, err := os.ReadFile(fname) // 使用 os.ReadFile (Go 1.16+)
if err != nil {
mylog.Error(err)
return []byte{}
}
return data
}

View File

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