go-gun/gun/peer.go

29 lines
600 B
Go
Raw Normal View History

2019-02-20 20:54:46 +00:00
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
}