/* * @Author: your name * @Date: 2021-10-21 22:36:25 * @LastEditTime: 2021-10-22 14:36:26 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \webrtc_easy_signal\proto.go */ package main import ( "encoding/json" "log" "sync" ) type Request struct { Type int `json:"type"` Data map[string]interface{} `json:"data"` } const ( REQ_LOGIN = 1000 REQ_INROOM = 1001 REQ_LEAVEROOM = 1002 REQ_CREATEROOM = 1003 REQ_LISTROOM = 1004 REQ_SENDSDP = 1005 ) type Peer struct { Name string // 用户名 Ws *WsConnection // websocket连接 Room *Room // 当前处在哪个房间 } type Room struct { Name string // 房间名称 Peers map[string]*Peer // 在房间里的节点 } var gmapRoom sync.Map var gmapUser sync.Map func get_peers_online(name string) *Peer { peer, ok := gmapUser.Load(name) if ok { return peer.(*Peer) } return nil } func get_interface_string(v interface{}) string { ret, ok := v.(string) if ok { return ret } return "" } func ProtoCallBack(ws *WsConnection, dat []byte) { if nil == ws { return } var payload Request e := json.Unmarshal(dat, &payload) if nil != e { log.Print(e.Error()) } log.Print(payload) switch payload.Type { case REQ_LOGIN: name := get_interface_string(payload.Data["name"]) if name != "" { if get_peers_online(name) != nil { log.Print("节点名重复") // todo 返回 return } peer := new(Peer) peer.Name = name peer.Ws = ws gmapUser.Store(name, peer) } case REQ_INROOM: log.Print(payload.Data["name"]) // 用户名称 log.Print(payload.Data["room_name"]) // 房间名称 if (payload.Data["room_name"] != "") && (payload.Data["name"] != "") { // 不存在房间就创建房间 if _, ok := gmapRoom.Load("room_name"); !ok { room := Room{ Name: payload.Data["room_name"].(string), } gmapRoom.Store(payload.Data["room_name"], Room{}) // if peer, ok := gmapUser.Load(payload.Data["name"]); ok { room.Peers[payload.Data["name"].(string)] = peer.(*Peer) } } else { // 将该用户添加进房间 } } break case REQ_CREATEROOM: break case REQ_LEAVEROOM: break case REQ_LISTROOM: break case REQ_SENDSDP: break } }