client. allow connection to any url. url generator function.

This commit is contained in:
Gennadii Kovalev 2016-05-20 14:26:18 +02:00
parent ae104e987d
commit b811737225
5 changed files with 32 additions and 9 deletions

View File

@ -140,7 +140,10 @@ var socket = io('ws://yourdomain.com', {transports: ['websocket']});
```go ```go
//connect to server, you can use your own transport settings //connect to server, you can use your own transport settings
c, err := gosocketio.Dial("localhost:80", transport.GetDefaultWebsocketTransport()) c, err := gosocketio.Dial(
gosocketio.GetUrl("localhost", 80, false),
transport.GetDefaultWebsocketTransport(),
)
//do something, handlers and functions are same as server ones //do something, handlers and functions are same as server ones

View File

@ -2,10 +2,12 @@ package gosocketio
import ( import (
"github.com/graarh/golang-socketio/transport" "github.com/graarh/golang-socketio/transport"
"strconv"
) )
const ( const (
webSocketProtocol = "ws://" webSocketProtocol = "ws://"
webSocketSecureProtocol = "wss://"
socketioUrl = "/socket.io/?EIO=3&transport=websocket" socketioUrl = "/socket.io/?EIO=3&transport=websocket"
) )
@ -17,16 +19,34 @@ type Client struct {
Channel Channel
} }
/**
Get ws/wss url by host and port
*/
func GetUrl(host string, port int, secure bool) string {
var prefix string
if secure {
prefix = webSocketSecureProtocol
} else {
prefix = webSocketProtocol
}
return prefix + host + ":" + strconv.Itoa(port) + socketioUrl
}
/** /**
connect to host and initialise socket.io protocol connect to host and initialise socket.io protocol
The correct ws protocol url example:
ws://myserver.com/socket.io/?EIO=3&transport=websocket
You can use GetUrlByHost for generating correct url
*/ */
func Dial(host string, tr transport.Transport) (*Client, error) { func Dial(url string, tr transport.Transport) (*Client, error) {
c := &Client{} c := &Client{}
c.initChannel() c.initChannel()
c.initMethods() c.initMethods()
var err error var err error
c.conn, err = tr.Connect(host) c.conn, err = tr.Connect(url)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -31,7 +31,9 @@ func sendJoin(c *gosocketio.Client) {
func main() { func main() {
runtime.GOMAXPROCS(runtime.NumCPU()) runtime.GOMAXPROCS(runtime.NumCPU())
c, err := gosocketio.Dial("localhost:3811", transport.GetDefaultWebsocketTransport()) c, err := gosocketio.Dial(
gosocketio.GetUrl("localhost", 3811, false),
transport.GetDefaultWebsocketTransport())
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

View File

@ -37,7 +37,7 @@ type Transport interface {
/** /**
Get client connection Get client connection
*/ */
Connect(host string) (conn Connection, err error) Connect(url string) (conn Connection, err error)
/** /**
Handle one server connection Handle one server connection

View File

@ -9,8 +9,6 @@ import (
) )
const ( const (
webSocketProtocol = "ws://"
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
upgradeFailed = "Upgrade failed: " upgradeFailed = "Upgrade failed: "
WsDefaultPingInterval = 30 * time.Second WsDefaultPingInterval = 30 * time.Second
@ -92,9 +90,9 @@ type WebsocketTransport struct {
BufferSize int BufferSize int
} }
func (wst *WebsocketTransport) Connect(host string) (conn Connection, err error) { func (wst *WebsocketTransport) Connect(url string) (conn Connection, err error) {
dialer := websocket.Dialer{} dialer := websocket.Dialer{}
socket, _, err := dialer.Dial(webSocketProtocol+host+socketioUrl, nil) socket, _, err := dialer.Dial(url, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }