mirror of
https://github.com/ChronosX88/go-gun.git
synced 2024-11-08 20:21:01 +00:00
29 lines
600 B
Go
29 lines
600 B
Go
|
package gun
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"net/url"
|
||
|
|
||
|
"github.com/gorilla/websocket"
|
||
|
)
|
||
|
|
||
|
type Peer interface {
|
||
|
Close() error
|
||
|
}
|
||
|
|
||
|
var PeerURLSchemes = map[string]func(context.Context, *url.URL) (Peer, error){
|
||
|
"ws": func(ctx context.Context, peerUrl *url.URL) (Peer, error) { return NewPeerWebSocket(ctx, peerUrl) },
|
||
|
}
|
||
|
|
||
|
type PeerWebSocket struct {
|
||
|
*websocket.Conn
|
||
|
}
|
||
|
|
||
|
func NewPeerWebSocket(ctx context.Context, peerUrl *url.URL) (*PeerWebSocket, error) {
|
||
|
conn, _, err := websocket.DefaultDialer.DialContext(ctx, peerUrl.String(), nil)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return &PeerWebSocket{conn}, nil
|
||
|
}
|