33 lines
722 B
Go
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
|
|
}
|