no message
parent
a0fdde18b0
commit
8dd4ce9f79
|
@ -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
|
||||
|
@ -38,9 +68,41 @@ func ProtoCallBack(ws *WsConnection,dat []byte) {
|
|||
log.Print(e.Error())
|
||||
}
|
||||
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:
|
||||
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:
|
||||
|
||||
|
|
|
@ -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"))
|
||||
}
|
|
@ -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,6 +215,7 @@ func (wsConn *WsConnection) wsWriteLoop() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
func (wsConn *WsConnection) close() {
|
||||
log.Println("关闭连接被调用了")
|
||||
|
|
Loading…
Reference in New Issue