123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- package WebSocket
- import (
- "ColdP_server/controllers/lib"
- "container/list"
- "encoding/json"
- "fmt"
- beego "github.com/beego/beego/v2/server/web"
- "github.com/gorilla/websocket"
- "net/http"
- "sync"
- "time"
- )
- // WebSocketController handles WebSocket requests.
- type WebSocketController struct {
- beego.Controller
- }
- // 网关 -》服务端 json 通用模板
- type Event_w struct {
- Type int // JOIN, LEAVE, MESSAGE
- Timestamp int // Unix timestamp (secs)
- Content interface{}
- }
- type Ms_Project struct {
- Cmd string `json:"Cmd"`
- Sn string `json:"Sn"`
- }
- // Event archives.
- var archive = list.New()
- type WsConn struct {
- Conn *websocket.Conn
- Mux sync.RWMutex
- }
- var (
- countryCapitalMap map[string]WsConn /*创建集合 */
- )
- func Join_wc(user string, ws *websocket.Conn) bool {
- // 有先加入 给全部人发消息
- _, ok := countryCapitalMap[user] /*如果确定是真实的,则存在,否则不存在 */
- if ok {
- fmt.Println(user + " 重复")
- countryCapitalMap[user] = WsConn{Conn: ws}
- return true
- } else {
- fmt.Println(user + " 注册成功")
- countryCapitalMap[user] = WsConn{Conn: ws}
- return false
- }
- }
- func Leave(user string) {
- fmt.Println("注销:" + user)
- for k, _ := range lib.CountrySnMap {
- _, ok := lib.CountrySnMap[k].Uuid_list[user]
- if ok {
- fmt.Println("清楚成功 用户! KEY:", k, " Uuid:", lib.CountrySnMap[k].Uuid_list[user])
- delete(lib.CountrySnMap[k].Uuid_list, user)
- if len(lib.CountrySnMap[k].Uuid_list) == 0 {
- fmt.Println("清楚成功 SN! KEY:", k)
- delete(lib.CountrySnMap, k)
- }
- }
- }
- delete(countryCapitalMap, user)
- }
- // This function handles all incoming chan messages.
- func chatroom() {
- countryCapitalMap = make(map[string]WsConn)
- }
- func init() {
- go chatroom()
- }
- // 连接 注册 Join method handles WebSocket requests for WebSocketController.
- func (this *WebSocketController) Join() {
- // 验证登录
- b_, admin_r := lib.Verification(this.Ctx.GetCookie("User_tokey"), this.GetString("User_tokey"))
- if !b_ {
- this.Redirect("/", 302)
- return
- }
- // Upgrade from http request to WebSocket.
- ws, err := websocket.Upgrade(this.Ctx.ResponseWriter, this.Ctx.Request, nil, 1024, 1024)
- if _, ok := err.(websocket.HandshakeError); ok {
- http.Error(this.Ctx.ResponseWriter, "Not a websocket handshake", 400)
- return
- } else if err != nil {
- fmt.Println("无法设置WebSocket连接:", err)
- return
- }
- // Join chat room.
- is := Join_wc(admin_r.T_uuid, ws)
- if !is {
- defer Leave(admin_r.T_uuid) // 退后 会自动执行
- time.Sleep(3 * time.Second)
- for {
- _, p, err := ws.ReadMessage()
- if err != nil {
- return
- }
- fmt.Println("============= WebSocket JSON =============")
- fmt.Println(admin_r.T_uuid, "收到信息:", string(p))
- var Ms_project Ms_Project
- err = json.Unmarshal(p, &Ms_project)
- if err != nil {
- fmt.Println("JSON反序列化失败[Ms_Project],err=", err)
- return
- }
- //fmt.Println("Cmd:", Ms_project.Cmd)
- fmt.Println("Sn:", Ms_project.Sn)
- //Parameter.Read_DeviceParameter(admin_r.T_uuid,Ms_project.Sn)
- _, ok := lib.CountrySnMap[Ms_project.Sn] /*如果确定是真实的,则存在,否则不存在 */
- if ok {
- } else {
- fmt.Println("CountrySnMap 没有,新建", Ms_project.Sn)
- lib.CountrySnMap[Ms_project.Sn] = lib.Cl_{
- Uuid_list: make(map[string]string),
- }
- }
- // 是否 有相同 用户
- _, ok = lib.CountrySnMap[Ms_project.Sn].Uuid_list[admin_r.T_uuid]
- if ok {
- fmt.Println("用户重复 ", admin_r.T_uuid)
- data, _ := json.Marshal(lib.JSONS{Code: 201, Msg: "用户重复!", Data: admin_r.T_uuid})
- Send_WebSocket(admin_r.T_uuid, string(data))
- } else {
- fmt.Println("用户新建 ", admin_r.T_uuid)
- lib.CountrySnMap[Ms_project.Sn].Uuid_list[admin_r.T_uuid] = admin_r.T_uuid
- data, _ := json.Marshal(lib.JSONS{Code: 200, Msg: "ok!", Data: admin_r.T_uuid})
- Send_WebSocket(admin_r.T_uuid, string(data))
- //lib.CountryRead_DeviceParameterSnMap[Ms_project.Sn] = ""
- }
- }
- } else {
- this.Redirect("/", 302)
- return
- }
- }
- /// ------------- ---------------------------------------------
- func Send_WebSocket(T_uuid string, T_json string) {
- ws, ok := countryCapitalMap[T_uuid] /*如果确定是真实的,则存在,否则不存在 */
- if ok && ws.Conn != nil {
- ws.Mux.Lock()
- if ws.Conn.WriteMessage(websocket.TextMessage, []byte(T_json)) != nil {
- println("ok!")
- }
- ws.Mux.Unlock()
- }
- }
|