golang-socketio/client.go

68 lines
1.2 KiB
Go
Raw Normal View History

2016-05-18 22:11:10 +02:00
package gosocketio
import (
"strconv"
"forge.cadoles.com/Pyxis/golang-socketio/transport"
2016-05-18 22:11:10 +02:00
)
const (
2018-09-18 18:01:28 +02:00
webSocketProtocol = "ws://"
webSocketSecureProtocol = "wss://"
2018-09-18 18:01:28 +02:00
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
2016-05-18 22:11:10 +02:00
)
/**
Socket.io client representation
*/
type Client struct {
methods
Channel
}
/**
Get ws/wss url by host and port
2018-09-18 18:01:28 +02:00
*/
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
}
2016-05-18 22:11:10 +02:00
/**
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
2016-05-18 22:11:10 +02:00
*/
func Dial(url string, tr transport.Transport) (*Client, error) {
2016-05-18 22:11:10 +02:00
c := &Client{}
c.initChannel()
c.initMethods()
var err error
c.conn, err = tr.Connect(url)
2016-05-18 22:11:10 +02:00
if err != nil {
return nil, err
}
go inLoop(&c.Channel, &c.methods)
go outLoop(&c.Channel, &c.methods)
go pinger(&c.Channel)
return c, nil
}
/**
Close client connection
*/
func (c *Client) Close() {
2017-05-10 18:27:25 +02:00
closeChannel(&c.Channel, &c.methods)
2016-05-18 22:11:10 +02:00
}