Обработка пакетов SDP от клиента
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
package socket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"g365sfu/logger"
|
||||
"g365sfu/sfu"
|
||||
connection "g365sfu/socket/struct"
|
||||
"g365sfu/utils"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/pion/webrtc/v4"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
@@ -31,7 +35,7 @@ func HandleWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||
// Канал для передачи байтов
|
||||
dataChan := make(chan []byte)
|
||||
|
||||
var connection = &Connection{
|
||||
var connection = &connection.Connection{
|
||||
Identificator: randomSocketIdentifier(),
|
||||
Socket: conn,
|
||||
}
|
||||
@@ -58,7 +62,7 @@ func randomSocketIdentifier() string {
|
||||
return "sock_" + utils.RandomString(10)
|
||||
}
|
||||
|
||||
func processData(data <-chan []byte, connection *Connection) {
|
||||
func processData(data <-chan []byte, connection *connection.Connection) {
|
||||
for bytes := range data {
|
||||
// Логика обработки байтов
|
||||
if bytes[0] == 0x01 {
|
||||
@@ -84,6 +88,35 @@ func processData(data <-chan []byte, connection *Connection) {
|
||||
return
|
||||
}
|
||||
|
||||
// Следующие типы сообщений
|
||||
// Создание комнаты
|
||||
if bytes[0] == 0x02 {
|
||||
room, _ := sfu.CreateRoom(connection)
|
||||
logger.LogSuccessMessage("room initializated " + room.RoomID)
|
||||
bytes = append([]byte{0x02}, []byte(room.RoomID)...)
|
||||
connection.Socket.WriteMessage(websocket.BinaryMessage, bytes)
|
||||
return
|
||||
}
|
||||
|
||||
//SDP OFFER для подключения к комнате
|
||||
if bytes[0] == 0x03 {
|
||||
roomIdLen := int(bytes[1])
|
||||
roomID := string(bytes[2 : 2+roomIdLen])
|
||||
_, exists := sfu.GetRoom(roomID)
|
||||
if !exists {
|
||||
logger.LogWarnMessage("peer " + connection.Socket.RemoteAddr().String() + " tried to join non existing room " + roomID)
|
||||
return
|
||||
}
|
||||
peerIdLen := int(bytes[2+roomIdLen])
|
||||
peerID := string(bytes[3+roomIdLen : 3+roomIdLen+peerIdLen])
|
||||
logger.LogSuccessMessage("peer " + connection.Socket.RemoteAddr().String() + " joined to room " + roomID)
|
||||
var offer webrtc.SessionDescription
|
||||
err := json.Unmarshal(bytes[3+roomIdLen+peerIdLen:], &offer)
|
||||
if err != nil {
|
||||
logger.LogWarnMessage("failed to unmarshal offer from peer " + connection.Socket.RemoteAddr().String() + ": " + err.Error())
|
||||
return
|
||||
}
|
||||
sfu.JoinWithOffer(roomID, peerID, offer)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user