You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.3 KiB

2 months ago
package socket
import (
"context"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"github.com/shopspring/decimal"
"matchmaking-system/internal/data"
"matchmaking-system/internal/data/redis"
"matchmaking-system/internal/pkg/flags"
"matchmaking-system/internal/pkg/logging/applogger"
"net/http"
"strconv"
"time"
)
const (
// Time allowed to read the next pong message from the peer.
pongWait = 60 * time.Second
// Send pings to peer with this period. Must be less than pongWait.
pingPeriod = (pongWait * 9) / 10
)
var upGraderOrder = websocket.Upgrader{
// Define read/write buffer size
WriteBufferSize: 1024,
ReadBufferSize: 1024,
// Verification request
CheckOrigin: func(r *http.Request) bool {
// If it is not a get request, return an error
if r.Method != "GET" {
applogger.Error("Request method error")
return false
}
var wss string
switch flags.CheckEnvironment {
case flags.CheckSport: // Spots Wss
wss = fmt.Sprintf("/%v", flags.SpotsWss)
case flags.CheckContract: // Contract Wss
wss = fmt.Sprintf("/%v", flags.ContractWss)
case flags.CheckSecond: // Second Wss
wss = fmt.Sprintf("/%v", flags.SecondWss)
case flags.CheckForex: // Forex Wss
wss = fmt.Sprintf("/%v", flags.ForexWss)
case flags.CheckMoney: // Money Wss
wss = fmt.Sprintf("/%v", flags.MoneyWss)
case flags.CheckShareUs: // Share Us Wss
wss = fmt.Sprintf("/%v", flags.ShareUsWss)
case flags.CheckShareMys: // Share Mys Wss
wss = fmt.Sprintf("/%v", flags.ShareMysWss)
case flags.CheckShareTha: // Share Tha Wss
wss = fmt.Sprintf("/%v", flags.ShareThaWss)
case flags.CheckShareIdn: // Share Idn Wss
wss = fmt.Sprintf("/%v", flags.ShareIdnWss)
case flags.CheckShareInr: // Share Inr Wss
wss = fmt.Sprintf("/%v", flags.ShareInrWss)
case flags.CheckShareSgd: // Share Sgd Wss
wss = fmt.Sprintf("/%v", flags.ShareSgdWss)
case flags.CheckShareHkd: // Share Hkd Wss
wss = fmt.Sprintf("/%v", flags.ShareHkdWss)
case flags.CheckShareGbx: // Share Gbx Wss
wss = fmt.Sprintf("/%v", flags.ShareGbxWss)
case flags.CheckShareEur: // Share Eur Wss
wss = fmt.Sprintf("/%v", flags.ShareEurWss)
case flags.CheckShareFur: // Share Fur Wss
wss = fmt.Sprintf("/%v", flags.ShareFurWss)
case flags.CheckShareBrl: // Share Brl Wss
wss = fmt.Sprintf("/%v", flags.ShareBrlWss)
case flags.CheckShareJpy: // Share Jpy Wss
wss = fmt.Sprintf("/%v", flags.ShareJpyWss)
case flags.CheckShareBlk: // share Blk Wss
wss = fmt.Sprintf("/%v", flags.ShareBlkWss)
case flags.CheckOptionInr: // Option Inr Wss
wss = fmt.Sprintf("/%v", flags.OptionInrWss)
case flags.CheckAdmin: // Admin Wss
wss = fmt.Sprintf("/%v", flags.AdminWss)
case flags.CheckAdminBlk: // Admin Blk Wss
wss = fmt.Sprintf("/%v", flags.AdminBlkWss)
}
if r.URL.Path != wss {
applogger.Error("Request path error")
return false
}
// Verification rules can also be customized according to other needs
return true
},
}
// PingMessage
// @Description:
type PingMessage struct {
Ping int64 `json:"ping"`
}
// SymbolMessage
// @Description:
type SymbolMessage struct {
Type string `json:"type"`
Symbol string `json:"symbol"`
Order []string `json:"order"`
}
// SymbolSumResult
// @Description:
type SymbolSumResult struct {
Symbol string `json:"Symbol"`
Price string `json:"Price"`
}
// SymbolUserSumResult
// @Description:
type SymbolUserSumResult struct {
Symbol string `json:"Symbol"`
UserSumPrice map[int64]decimal.Decimal `json:"UserSumPrice"`
}
// UserMarketStatistics
// @Description:
type UserMarketStatistics struct {
UserMarkerSubscribe string `json:"UserMarkerSubscribe"` // 用户市场订阅类型
UserMarketTotalAssets string `json:"UserMarketTotalAssets"` // 用户市场总资产
UserMarketAvailable string `json:"UserMarketAvailable"` // 用户市场总可用
UserMarketFreeze string `json:"UserMarketFreeze"` // 用户市场冻结
UserMarketProfitAndLoss string `json:"UserMarketProfitAndLoss"` // 用户市场累计盈亏
UserMarketTotalFee string `json:"UserMarketTotalFee"` // 用户市场总手续费
UserMarketFloatingPL string `json:"UserMarketFloatingPL"` // 用户市场总浮动盈亏
}
// ParsePingMessage
//
// @Description: ping message
// @param message
// @return *PingMessage
func ParsePingMessage(message string) *PingMessage {
result := PingMessage{}
err := json.Unmarshal([]byte(message), &result)
if err != nil {
return nil
}
return &result
}
// SubMessage
//
// @Description: 订阅消息解析
// @param message
// @return *SymbolMessage
func SubMessage(message string) *SymbolMessage {
result := SymbolMessage{}
err := json.Unmarshal([]byte(message), &result)
if err != nil {
return nil
}
return &result
}
// ReturnValue
//
// @Description: 构建推送pong
// @param str
// @return string
func ReturnValue(str string) string {
pongMsg := fmt.Sprintf("{\"type\": \"%v\"}", str)
return pongMsg
}
// GetUserIdByToken
//
// @Description: 通过UserId验证Token
// @param token
// @return int64
// @return error
func GetUserIdByToken(token string) (int64, error) {
userId, err := redis.GetCacheData(context.Background(), data.Reds, token)
if err != nil || len(userId) == 0 {
return 0, err
}
userID, err := strconv.Atoi(userId)
if err != nil || userID == 0 {
return 0, err
}
return int64(userID), nil
}