multimedia/server/webrtc_easy_signal/callbacks.go

121 lines
2.3 KiB
Go
Raw Normal View History

2021-10-21 15:51:34 +00:00
/*
* @Author: your name
* @Date: 2021-10-21 22:36:25
2021-10-22 08:10:13 +00:00
* @LastEditTime: 2021-10-22 14:36:26
2021-10-21 15:51:34 +00:00
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \webrtc_easy_signal\proto.go
*/
package main
import (
"encoding/json"
"log"
"sync"
)
2021-10-22 08:10:13 +00:00
type Request struct {
Type int `json:"type"`
2021-10-21 15:51:34 +00:00
Data map[string]interface{} `json:"data"`
}
2021-10-22 08:10:13 +00:00
2021-10-21 15:51:34 +00:00
const (
2021-10-22 08:10:13 +00:00
REQ_LOGIN = 1000
REQ_INROOM = 1001
REQ_LEAVEROOM = 1002
2021-10-21 15:51:34 +00:00
REQ_CREATEROOM = 1003
2021-10-22 08:10:13 +00:00
REQ_LISTROOM = 1004
REQ_SENDSDP = 1005
2021-10-21 15:51:34 +00:00
)
2021-10-22 08:10:13 +00:00
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 ""
}
2021-10-21 15:51:34 +00:00
2021-10-22 08:10:13 +00:00
func ProtoCallBack(ws *WsConnection, dat []byte) {
if nil == ws {
2021-10-21 15:51:34 +00:00
return
}
var payload Request
e := json.Unmarshal(dat, &payload)
if nil != e {
log.Print(e.Error())
}
2021-10-22 08:10:13 +00:00
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)
}
2021-10-21 15:51:34 +00:00
case REQ_INROOM:
2021-10-22 08:10:13 +00:00
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 {
// 将该用户添加进房间
}
}
2021-10-21 15:51:34 +00:00
break
case REQ_CREATEROOM:
break
case REQ_LEAVEROOM:
break
case REQ_LISTROOM:
break
case REQ_SENDSDP:
2021-10-22 08:10:13 +00:00
break
2021-10-21 15:51:34 +00:00
}
2021-10-22 08:10:13 +00:00
}