no message
parent
a0fdde18b0
commit
8dd4ce9f79
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* @Author: your name
|
* @Author: your name
|
||||||
* @Date: 2021-10-21 22:36:25
|
* @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
|
* @LastEditors: Please set LastEditors
|
||||||
* @Description: In User Settings Edit
|
* @Description: In User Settings Edit
|
||||||
* @FilePath: \webrtc_easy_signal\proto.go
|
* @FilePath: \webrtc_easy_signal\proto.go
|
||||||
|
@ -14,22 +14,52 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Request struct{
|
type Request struct {
|
||||||
Type int `json:"type"`
|
Type int `json:"type"`
|
||||||
Data map[string]interface{} `json:"data"`
|
Data map[string]interface{} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
REQ_INROOM = 1001
|
REQ_LOGIN = 1000
|
||||||
REQ_LEAVEROOM = 1002
|
REQ_INROOM = 1001
|
||||||
|
REQ_LEAVEROOM = 1002
|
||||||
REQ_CREATEROOM = 1003
|
REQ_CREATEROOM = 1003
|
||||||
REQ_LISTROOM = 1004
|
REQ_LISTROOM = 1004
|
||||||
REQ_SENDSDP = 1005
|
REQ_SENDSDP = 1005
|
||||||
)
|
)
|
||||||
|
|
||||||
var gmap sync.Map
|
type Peer struct {
|
||||||
|
Name string // 用户名
|
||||||
|
Ws *WsConnection // websocket连接
|
||||||
|
Room *Room // 当前处在哪个房间
|
||||||
|
}
|
||||||
|
|
||||||
func ProtoCallBack(ws *WsConnection,dat []byte) {
|
type Room struct {
|
||||||
if nil == ws{
|
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
|
return
|
||||||
}
|
}
|
||||||
var payload Request
|
var payload Request
|
||||||
|
@ -37,10 +67,42 @@ func ProtoCallBack(ws *WsConnection,dat []byte) {
|
||||||
if nil != e {
|
if nil != e {
|
||||||
log.Print(e.Error())
|
log.Print(e.Error())
|
||||||
}
|
}
|
||||||
log.Print(payload)
|
log.Print(payload)
|
||||||
switch(payload.Type){
|
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:
|
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
|
break
|
||||||
case REQ_CREATEROOM:
|
case REQ_CREATEROOM:
|
||||||
|
|
||||||
|
@ -53,6 +115,6 @@ func ProtoCallBack(ws *WsConnection,dat []byte) {
|
||||||
break
|
break
|
||||||
case REQ_SENDSDP:
|
case REQ_SENDSDP:
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* @Author: your name
|
* @Author: your name
|
||||||
* @Date: 2021-10-21 20:30:24
|
* @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
|
* @LastEditors: Please set LastEditors
|
||||||
* @Description: In User Settings Edit
|
* @Description: In User Settings Edit
|
||||||
* @FilePath: \webrtc_easy_signal\main.go
|
* @FilePath: \webrtc_easy_signal\main.go
|
||||||
|
@ -10,6 +10,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,6 +21,7 @@ func StartWebsocket(addrPort string) {
|
||||||
http.ListenAndServe(addrPort, nil)
|
http.ListenAndServe(addrPort, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main(){
|
func main() {
|
||||||
|
log.Print("server started")
|
||||||
StartWebsocket(fmt.Sprintf("0.0.0.0:9555"))
|
StartWebsocket(fmt.Sprintf("0.0.0.0:9555"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var maxConnId int64
|
var maxConnId int64
|
||||||
|
|
||||||
// 用于广播
|
// 用于广播
|
||||||
var WsConnAll map[int64]*WsConnection
|
var WsConnAll map[int64]*WsConnection
|
||||||
|
|
||||||
|
@ -126,7 +127,7 @@ func (wsConn *WsConnection) processLoop() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err.Error() == "超时" {
|
if err.Error() == "超时" {
|
||||||
// log.Print(wsConn.lastHeartBeatTime.String())
|
// log.Print(wsConn.lastHeartBeatTime.String())
|
||||||
if(wsConn.needHeartBeat){
|
if wsConn.needHeartBeat {
|
||||||
if time.Now().Sub(wsConn.lastHeartBeatTime) > time.Second*15 {
|
if time.Now().Sub(wsConn.lastHeartBeatTime) > time.Second*15 {
|
||||||
log.Print("心跳超时")
|
log.Print("心跳超时")
|
||||||
wsConn.close()
|
wsConn.close()
|
||||||
|
@ -136,13 +137,15 @@ func (wsConn *WsConnection) processLoop() {
|
||||||
}
|
}
|
||||||
break
|
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"`
|
Payload interface{} `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送信息
|
// 发送信息
|
||||||
func (wsConn *WsConnection) SendPayload(ptype int64, v interface{}) error {
|
func (wsConn *WsConnection) SendPayload(ptype int64, v interface{}) error {
|
||||||
var resp Response
|
var resp Response
|
||||||
|
@ -212,12 +215,13 @@ func (wsConn *WsConnection) wsWriteLoop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 关闭连接
|
// 关闭连接
|
||||||
func (wsConn *WsConnection) close() {
|
func (wsConn *WsConnection) close() {
|
||||||
log.Println("关闭连接被调用了")
|
log.Println("关闭连接被调用了")
|
||||||
wsConn.wsSocket.Close()
|
wsConn.wsSocket.Close()
|
||||||
wsConn.mutex.Lock()
|
wsConn.mutex.Lock()
|
||||||
|
|
||||||
defer wsConn.mutex.Unlock()
|
defer wsConn.mutex.Unlock()
|
||||||
if wsConn.isClosed == false {
|
if wsConn.isClosed == false {
|
||||||
wsConn.isClosed = true
|
wsConn.isClosed = true
|
||||||
|
|
Loading…
Reference in New Issue