Files
g365sfu/socket/socket.go
2026-03-09 16:21:28 +02:00

48 lines
1013 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package socket
import (
"fmt"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
// Разрешаем со всех origin
return true
},
}
// Обработчик WebSocket соединений
func HandleWebSocket(w http.ResponseWriter, r *http.Request) {
conn, _ := upgrader.Upgrade(w, r, nil)
defer conn.Close()
// Канал для передачи байтов
dataChan := make(chan []byte)
// Запуск обработчика в горутине
go processData(dataChan)
for {
messageType, p, err := conn.ReadMessage()
if err != nil || messageType != websocket.BinaryMessage {
break
}
// Передача байтов для обработки
dataChan <- p
}
}
func processData(data <-chan []byte) {
for bytes := range data {
// Логика обработки байтов
if bytes[0] == 0x01 {
fmt.Print("Type 0x01 received")
}
}
}