socket.io library. current version.
This commit is contained in:
51
transport/transport.go
Normal file
51
transport/transport.go
Normal file
@ -0,0 +1,51 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
/**
|
||||
End-point connection for given transport
|
||||
*/
|
||||
type Connection interface {
|
||||
/**
|
||||
Receive one more message, block until received
|
||||
*/
|
||||
GetMessage() (message string, err error)
|
||||
|
||||
/**
|
||||
Send given message, block until sent
|
||||
*/
|
||||
WriteMessage(message string) error
|
||||
|
||||
/**
|
||||
Close current connection
|
||||
*/
|
||||
Close()
|
||||
|
||||
/**
|
||||
Get ping time interval and ping request timeout
|
||||
*/
|
||||
PingParams() (interval, timeout time.Duration)
|
||||
}
|
||||
|
||||
/**
|
||||
Connection factory for given transport
|
||||
*/
|
||||
type Transport interface {
|
||||
/**
|
||||
Get client connection
|
||||
*/
|
||||
Connect(host string) (conn Connection, err error)
|
||||
|
||||
/**
|
||||
Handle one server connection
|
||||
*/
|
||||
HandleConnection(w http.ResponseWriter, r *http.Request) (conn Connection, err error)
|
||||
|
||||
/**
|
||||
Serve HTTP request after making connection and events setup
|
||||
*/
|
||||
Serve(w http.ResponseWriter, r *http.Request)
|
||||
}
|
138
transport/websocket.go
Normal file
138
transport/websocket.go
Normal file
@ -0,0 +1,138 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/gorilla/websocket"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
webSocketProtocol = "ws://"
|
||||
socketioUrl = "/socket.io/?EIO=3&transport=websocket"
|
||||
upgradeFailed = "Upgrade failed: "
|
||||
|
||||
WsDefaultPingInterval = 30 * time.Second
|
||||
WsDefaultPingTimeout = 60 * time.Second
|
||||
WsDefaultReceiveTimeout = 60 * time.Second
|
||||
WsDefaultSendTimeout = 60 * time.Second
|
||||
WsDefaultBufferSize = 1024 * 32
|
||||
)
|
||||
|
||||
var (
|
||||
ErrorBinaryMessage = errors.New("Binary messages are not supported")
|
||||
ErrorBadBuffer = errors.New("Buffer error")
|
||||
ErrorPacketWrong = errors.New("Wrong packet type error")
|
||||
ErrorMethodNotAllowed = errors.New("Method not allowed")
|
||||
ErrorHttpUpgradeFailed = errors.New("Http upgrade failed")
|
||||
)
|
||||
|
||||
type WebsocketConnection struct {
|
||||
socket *websocket.Conn
|
||||
transport *WebsocketTransport
|
||||
}
|
||||
|
||||
func (wsc *WebsocketConnection) GetMessage() (message string, err error) {
|
||||
wsc.socket.SetReadDeadline(time.Now().Add(wsc.transport.ReceiveTimeout))
|
||||
msgType, reader, err := wsc.socket.NextReader()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
//support only text messages exchange
|
||||
if msgType != websocket.TextMessage {
|
||||
return "", ErrorBinaryMessage
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadAll(reader)
|
||||
if err != nil {
|
||||
return "", ErrorBadBuffer
|
||||
}
|
||||
text := string(data)
|
||||
|
||||
//empty messages are not allowed
|
||||
if len(text) == 0 {
|
||||
return "", ErrorPacketWrong
|
||||
}
|
||||
|
||||
return text, nil
|
||||
}
|
||||
|
||||
func (wsc *WebsocketConnection) WriteMessage(message string) error {
|
||||
wsc.socket.SetWriteDeadline(time.Now().Add(wsc.transport.SendTimeout))
|
||||
writer, err := wsc.socket.NextWriter(websocket.TextMessage)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := writer.Write([]byte(message)); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := writer.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (wsc *WebsocketConnection) Close() {
|
||||
wsc.socket.Close()
|
||||
}
|
||||
|
||||
func (wsc *WebsocketConnection) PingParams() (interval, timeout time.Duration) {
|
||||
return wsc.transport.PingInterval, wsc.transport.PingTimeout
|
||||
}
|
||||
|
||||
type WebsocketTransport struct {
|
||||
PingInterval time.Duration
|
||||
PingTimeout time.Duration
|
||||
ReceiveTimeout time.Duration
|
||||
SendTimeout time.Duration
|
||||
|
||||
BufferSize int
|
||||
}
|
||||
|
||||
func (wst *WebsocketTransport) Connect(host string) (conn Connection, err error) {
|
||||
dialer := websocket.Dialer{}
|
||||
socket, _, err := dialer.Dial(webSocketProtocol+host+socketioUrl, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &WebsocketConnection{socket, wst}, nil
|
||||
}
|
||||
|
||||
func (wst *WebsocketTransport) HandleConnection(
|
||||
w http.ResponseWriter, r *http.Request) (conn Connection, err error) {
|
||||
|
||||
if r.Method != "GET" {
|
||||
http.Error(w, upgradeFailed+ErrorMethodNotAllowed.Error(), 503)
|
||||
return nil, ErrorMethodNotAllowed
|
||||
}
|
||||
|
||||
socket, err := websocket.Upgrade(w, r, nil, wst.BufferSize, wst.BufferSize)
|
||||
if err != nil {
|
||||
http.Error(w, upgradeFailed+err.Error(), 503)
|
||||
return nil, ErrorHttpUpgradeFailed
|
||||
}
|
||||
|
||||
return &WebsocketConnection{socket, wst}, nil
|
||||
}
|
||||
|
||||
/**
|
||||
Websocket connection do not require any additional processing
|
||||
*/
|
||||
func (wst *WebsocketTransport) Serve(w http.ResponseWriter, r *http.Request) {}
|
||||
|
||||
/**
|
||||
Returns websocket connection with default params
|
||||
*/
|
||||
func GetDefaultWebsocketTransport() *WebsocketTransport {
|
||||
return &WebsocketTransport{
|
||||
PingInterval: WsDefaultPingInterval,
|
||||
PingTimeout: WsDefaultPingTimeout,
|
||||
ReceiveTimeout: WsDefaultReceiveTimeout,
|
||||
SendTimeout: WsDefaultSendTimeout,
|
||||
BufferSize: WsDefaultBufferSize,
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user