client. allow connection to any url. url generator function.
This commit is contained in:
parent
ae104e987d
commit
b811737225
|
@ -140,7 +140,10 @@ var socket = io('ws://yourdomain.com', {transports: ['websocket']});
|
|||
|
||||
```go
|
||||
//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
|
||||
|
||||
|
|
26
client.go
26
client.go
|
@ -2,10 +2,12 @@ package gosocketio
|
|||
|
||||
import (
|
||||
"github.com/graarh/golang-socketio/transport"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
webSocketProtocol = "ws://"
|
||||
webSocketSecureProtocol = "wss://"
|
||||
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
|
||||
)
|
||||
|
||||
|
@ -18,15 +20,33 @@ type Client struct {
|
|||
}
|
||||
|
||||
/**
|
||||
connect to host and initialise socket.io protocol
|
||||
Get ws/wss url by host and port
|
||||
*/
|
||||
func Dial(host string, tr transport.Transport) (*Client, error) {
|
||||
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
|
||||
|
||||
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(url string, tr transport.Transport) (*Client, error) {
|
||||
c := &Client{}
|
||||
c.initChannel()
|
||||
c.initMethods()
|
||||
|
||||
var err error
|
||||
c.conn, err = tr.Connect(host)
|
||||
c.conn, err = tr.Connect(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -31,7 +31,9 @@ func sendJoin(c *gosocketio.Client) {
|
|||
func main() {
|
||||
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 {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ type Transport interface {
|
|||
/**
|
||||
Get client connection
|
||||
*/
|
||||
Connect(host string) (conn Connection, err error)
|
||||
Connect(url string) (conn Connection, err error)
|
||||
|
||||
/**
|
||||
Handle one server connection
|
||||
|
|
|
@ -9,8 +9,6 @@ import (
|
|||
)
|
||||
|
||||
const (
|
||||
webSocketProtocol = "ws://"
|
||||
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
|
||||
upgradeFailed = "Upgrade failed: "
|
||||
|
||||
WsDefaultPingInterval = 30 * time.Second
|
||||
|
@ -92,9 +90,9 @@ type WebsocketTransport struct {
|
|||
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{}
|
||||
socket, _, err := dialer.Dial(webSocketProtocol+host+socketioUrl, nil)
|
||||
socket, _, err := dialer.Dial(url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue