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.
75 lines
2.0 KiB
75 lines
2.0 KiB
package us
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"wss-pool/cmd/websocketcollect/cache"
|
|
"wss-pool/logging/applogger"
|
|
)
|
|
|
|
// SubscribeShareUs
|
|
//
|
|
// @Description: 广播美股实时行情/新增code
|
|
// @param addr
|
|
func SubscribeShareUs(ipServer, addrServer string) {
|
|
// 初始化相同项目mongodb和redis的ip对应关系
|
|
//data.MgoDbToRedisMap = common.GetMgoDbToRedisMap(config.Config.Mongodb.RedisToMongodb)
|
|
// 初始化所有项目MongoDB
|
|
//data.Mgo_initMap(config.Config.Mongodb, common.GetMongodbAddrList(config.Config.Mongodb.AddrList))
|
|
// 初始化美股
|
|
//InitShareUsCode()
|
|
cache.InitDB("us")
|
|
hub := newHub()
|
|
go hub.ShareMarket()
|
|
go hub.run()
|
|
http.HandleFunc("/usWss/add/code", serveHome)
|
|
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
|
handleConnection(hub, w, r)
|
|
})
|
|
|
|
addr := fmt.Sprintf("%v%v", ipServer, addrServer)
|
|
|
|
applogger.Info("wss-pool server start at %v", addr)
|
|
if err := http.ListenAndServe(addr, nil); err != nil {
|
|
applogger.Error("ListenAndServe:%v", err)
|
|
}
|
|
}
|
|
|
|
// serveHome
|
|
//
|
|
// @Description: 新增美股code
|
|
// @param w
|
|
// @param r
|
|
func serveHome(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/usWss/add/code" {
|
|
http.Error(w, "Not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
value := r.FormValue("code")
|
|
if value == "" {
|
|
http.Error(w, "code is empty", http.StatusBadRequest)
|
|
return
|
|
}
|
|
applogger.Info("subscribe share us code:%v", value)
|
|
if err := cache.WriteDB(value, value); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// 确保返回 JSON 内容类型
|
|
w.Header().Set("Content-Type", "application/json")
|
|
// 设置状态码
|
|
w.WriteHeader(http.StatusOK)
|
|
// 将响应编码为 JSON 并写入响应体
|
|
if err := json.NewEncoder(w).Encode(Message{
|
|
Topic: value,
|
|
Content: "新增美股code成功!",
|
|
}); err != nil {
|
|
http.Error(w, "Failed to encode JSON", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|