From 8dd4ce9f79b2dc17cd0bb6f7610192abc4daae62 Mon Sep 17 00:00:00 2001 From: "ATTIOT\\zhengcy" Date: Fri, 22 Oct 2021 16:10:13 +0800 Subject: [PATCH] no message --- server/webrtc_easy_signal/callbacks.go | 92 +++++++++++++++++++---- server/webrtc_easy_signal/main.go | 8 +- server/webrtc_easy_signal/ws_conection.go | 14 ++-- 3 files changed, 91 insertions(+), 23 deletions(-) diff --git a/server/webrtc_easy_signal/callbacks.go b/server/webrtc_easy_signal/callbacks.go index 4a73101..55d34c4 100644 --- a/server/webrtc_easy_signal/callbacks.go +++ b/server/webrtc_easy_signal/callbacks.go @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-10-21 22:36:25 - * @LastEditTime: 2021-10-21 23:01:28 + * @LastEditTime: 2021-10-22 14:36:26 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \webrtc_easy_signal\proto.go @@ -14,22 +14,52 @@ import ( "sync" ) -type Request struct{ - Type int `json:"type"` +type Request struct { + Type int `json:"type"` Data map[string]interface{} `json:"data"` } + const ( - REQ_INROOM = 1001 - REQ_LEAVEROOM = 1002 + REQ_LOGIN = 1000 + REQ_INROOM = 1001 + REQ_LEAVEROOM = 1002 REQ_CREATEROOM = 1003 - REQ_LISTROOM = 1004 - REQ_SENDSDP = 1005 + REQ_LISTROOM = 1004 + REQ_SENDSDP = 1005 ) -var gmap sync.Map +type Peer struct { + Name string // 用户名 + Ws *WsConnection // websocket连接 + Room *Room // 当前处在哪个房间 +} -func ProtoCallBack(ws *WsConnection,dat []byte) { - if nil == ws{ +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 @@ -37,10 +67,42 @@ func ProtoCallBack(ws *WsConnection,dat []byte) { if nil != e { log.Print(e.Error()) } - log.Print(payload) - switch(payload.Type){ + 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["message"]) + 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: @@ -53,6 +115,6 @@ func ProtoCallBack(ws *WsConnection,dat []byte) { break case REQ_SENDSDP: - break + break } -} \ No newline at end of file +} diff --git a/server/webrtc_easy_signal/main.go b/server/webrtc_easy_signal/main.go index 23775a8..1a6f302 100644 --- a/server/webrtc_easy_signal/main.go +++ b/server/webrtc_easy_signal/main.go @@ -1,7 +1,7 @@ /* * @Author: your name * @Date: 2021-10-21 20:30:24 - * @LastEditTime: 2021-10-21 22:52:46 + * @LastEditTime: 2021-10-22 10:47:26 * @LastEditors: Please set LastEditors * @Description: In User Settings Edit * @FilePath: \webrtc_easy_signal\main.go @@ -10,6 +10,7 @@ package main import ( "fmt" + "log" "net/http" ) @@ -20,6 +21,7 @@ func StartWebsocket(addrPort string) { http.ListenAndServe(addrPort, nil) } -func main(){ +func main() { + log.Print("server started") StartWebsocket(fmt.Sprintf("0.0.0.0:9555")) -} \ No newline at end of file +} diff --git a/server/webrtc_easy_signal/ws_conection.go b/server/webrtc_easy_signal/ws_conection.go index 8e22675..af8c89b 100644 --- a/server/webrtc_easy_signal/ws_conection.go +++ b/server/webrtc_easy_signal/ws_conection.go @@ -23,6 +23,7 @@ const ( ) var maxConnId int64 + // 用于广播 var WsConnAll map[int64]*WsConnection @@ -126,7 +127,7 @@ func (wsConn *WsConnection) processLoop() { if err != nil { if err.Error() == "超时" { // log.Print(wsConn.lastHeartBeatTime.String()) - if(wsConn.needHeartBeat){ + if wsConn.needHeartBeat { if time.Now().Sub(wsConn.lastHeartBeatTime) > time.Second*15 { log.Print("心跳超时") wsConn.close() @@ -136,13 +137,15 @@ func (wsConn *WsConnection) processLoop() { } break } - ProtoCallBack(wsConn,msg.data) + ProtoCallBack(wsConn, msg.data) } } -type Response struct{ - Type int `json:"type"` + +type Response struct { + Type int `json:"type"` Payload interface{} `json:"data"` } + // 发送信息 func (wsConn *WsConnection) SendPayload(ptype int64, v interface{}) error { var resp Response @@ -212,12 +215,13 @@ func (wsConn *WsConnection) wsWriteLoop() { } } } + // 关闭连接 func (wsConn *WsConnection) close() { log.Println("关闭连接被调用了") wsConn.wsSocket.Close() wsConn.mutex.Lock() - + defer wsConn.mutex.Unlock() if wsConn.isClosed == false { wsConn.isClosed = true