2016-05-18 22:11:10 +02:00
|
|
|
package gosocketio
|
|
|
|
|
|
|
|
import (
|
2016-05-20 14:26:18 +02:00
|
|
|
"strconv"
|
2018-09-18 17:59:07 +02:00
|
|
|
|
|
|
|
"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://"
|
2016-05-20 14:26:18 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-05-20 14:26:18 +02:00
|
|
|
/**
|
|
|
|
Get ws/wss url by host and port
|
2018-09-18 18:01:28 +02:00
|
|
|
*/
|
2016-05-20 14:26:18 +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
|
2016-05-20 14:26:18 +02:00
|
|
|
|
|
|
|
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
|
|
|
*/
|
2016-05-20 14:26:18 +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
|
2016-05-20 14:26:18 +02:00
|
|
|
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
|
|
|
}
|