admin_backend/util/websocket.go
2026-01-26 15:28:47 +08:00

33 lines
722 B
Go

package util
import (
"fmt"
"net"
"time"
"golang.org/x/net/websocket"
)
func GetWebsocket(AppId, ServerId int) (*websocket.Conn, error) {
ServerConfig, _ := GetServerConfig(AppId, ServerId)
origin := "http://localhost/"
if AppId == 0 {
ServerConfig.Host = "google.bywaystudios.com"
}
url := fmt.Sprintf("ws://%s:%d/", ServerConfig.Host, ServerConfig.WsPort)
config, err := websocket.NewConfig(url, origin)
if err != nil {
return nil, fmt.Errorf("failed to create websocket config: %v", err)
}
config.Dialer = &net.Dialer{
Timeout: 5 * time.Second,
}
ws, err := websocket.DialConfig(config)
if err != nil {
return nil, fmt.Errorf("failed to connect to websocket: %v", err)
}
return ws, nil
}