go-emlid/reach/client/socketio/transport.go

47 lines
1.0 KiB
Go

package socketio
import (
"net"
"net/http"
"forge.cadoles.com/Pyxis/golang-socketio/transport"
"github.com/gorilla/websocket"
)
type Transport struct {
dial DialFunc
ws *transport.WebsocketTransport
}
// Connect implements transport.Transport.
func (t *Transport) Connect(url string) (conn transport.Connection, err error) {
if t.dial == nil {
return t.ws.Connect(url)
} else {
dialer := websocket.Dialer{
NetDial: func(network, addr string) (net.Conn, error) {
return t.dial(network, addr)
},
}
socket, _, err := dialer.Dial(url, t.ws.RequestHeader)
if err != nil {
return nil, err
}
return transport.NewWebsocketConnection(socket, t.ws), nil
}
}
// HandleConnection implements transport.Transport.
func (t *Transport) HandleConnection(w http.ResponseWriter, r *http.Request) (conn transport.Connection, err error) {
return t.ws.HandleConnection(w, r)
}
// Serve implements transport.Transport.
func (t *Transport) Serve(w http.ResponseWriter, r *http.Request) {
t.ws.Serve(w, r)
}
var _ transport.Transport = &Transport{}