From 791a697df58f03a828f54b1c82b178e3dc268f9d Mon Sep 17 00:00:00 2001 From: Andrew Yasinsky Date: Tue, 19 Sep 2017 09:29:10 -0700 Subject: [PATCH 1/2] added socket url param --- client.go | 30 ++++++++++++++++++++++++------ handler.go | 4 ++-- loop.go | 4 ++-- send.go | 2 +- server.go | 6 +++--- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/client.go b/client.go index deded6c..c9f8fba 100644 --- a/client.go +++ b/client.go @@ -1,14 +1,17 @@ package gosocketio import ( - "github.com/graarh/golang-socketio/transport" + "fmt" + "github.com/n0needt0/golang-socketio/transport" + "net/url" "strconv" + "strings" ) const ( - webSocketProtocol = "ws://" + webSocketProtocol = "ws://" webSocketSecureProtocol = "wss://" - socketioUrl = "/socket.io/?EIO=3&transport=websocket" + socketioUrl = "/socket.io/?EIO=3&transport=websocket" ) /** @@ -21,15 +24,30 @@ type Client struct { /** Get ws/wss url by host and port - */ -func GetUrl(host string, port int, secure bool) string { +*/ +func GetUrl(host string, port int, params []string, secure bool) string { var prefix string if secure { prefix = webSocketSecureProtocol } else { prefix = webSocketProtocol } - return prefix + host + ":" + strconv.Itoa(port) + socketioUrl + + _url, err := url.Parse(prefix + host + ":" + strconv.Itoa(port) + socketioUrl) + if err != nil { + fmt.Println("We unable to parse given url: ", _url) + } + + if len(params) > 0 { + _uval := _url.Query() + for _, element := range params { + s := strings.Split(element, "=") + _uval.Add(s[0], s[1]) + } + _url.RawQuery = _uval.Encode() + } + + return _url.String() } /** diff --git a/handler.go b/handler.go index 78c3636..07ccf48 100644 --- a/handler.go +++ b/handler.go @@ -2,9 +2,9 @@ package gosocketio import ( "encoding/json" - "github.com/graarh/golang-socketio/protocol" - "sync" + "github.com/n0needt0/golang-socketio/protocol" "reflect" + "sync" ) const ( diff --git a/loop.go b/loop.go index 5e73c5b..0c65857 100644 --- a/loop.go +++ b/loop.go @@ -3,8 +3,8 @@ package gosocketio import ( "encoding/json" "errors" - "github.com/graarh/golang-socketio/protocol" - "github.com/graarh/golang-socketio/transport" + "github.com/n0needt0/golang-socketio/protocol" + "github.com/n0needt0/golang-socketio/transport" "net/http" "sync" "time" diff --git a/send.go b/send.go index 07e8130..db8d777 100644 --- a/send.go +++ b/send.go @@ -3,7 +3,7 @@ package gosocketio import ( "encoding/json" "errors" - "github.com/graarh/golang-socketio/protocol" + "github.com/n0needt0/golang-socketio/protocol" "log" "time" ) diff --git a/server.go b/server.go index 7583633..ca3272d 100644 --- a/server.go +++ b/server.go @@ -7,8 +7,8 @@ import ( "encoding/json" "errors" "fmt" - "github.com/graarh/golang-socketio/protocol" - "github.com/graarh/golang-socketio/transport" + "github.com/n0needt0/golang-socketio/protocol" + "github.com/n0needt0/golang-socketio/transport" "math/rand" "net/http" "sync" @@ -43,7 +43,7 @@ type Server struct { /** Close current channel - */ +*/ func (c *Channel) Close() { if c.server != nil { closeChannel(c, &c.server.methods) From 79c2718a715dd8398c621fb1f2c7b5ad910a7138 Mon Sep 17 00:00:00 2001 From: Andrew Yasinsky Date: Wed, 25 Oct 2017 20:50:44 -0700 Subject: [PATCH 2/2] Added request methoud --- loop.go | 7 ++++--- server.go | 12 ++++++++++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/loop.go b/loop.go index 0c65857..a2da23e 100644 --- a/loop.go +++ b/loop.go @@ -48,9 +48,10 @@ type Channel struct { ack ackProcessor - server *Server - ip string - requestHeader http.Header + server *Server + ip string + requestHeader http.Header + requestRequest http.Request } /** diff --git a/server.go b/server.go index ca3272d..6341bbf 100644 --- a/server.go +++ b/server.go @@ -68,6 +68,13 @@ func (c *Channel) RequestHeader() http.Header { return c.requestHeader } +/** +Get request uri of this connection +*/ +func (c *Channel) GetRequest() http.Request { + return c.requestRequest +} + /** Get channel by it's sid */ @@ -304,7 +311,7 @@ func (s *Server) SendOpenSequence(c *Channel) { Setup event loop for given connection */ func (s *Server) SetupEventLoop(conn transport.Connection, remoteAddr string, - requestHeader http.Header) { + requestHeader http.Header, requestRequest http.Request) { interval, timeout := conn.PingParams() hdr := Header{ @@ -318,6 +325,7 @@ func (s *Server) SetupEventLoop(conn transport.Connection, remoteAddr string, c.conn = conn c.ip = remoteAddr c.requestHeader = requestHeader + c.requestRequest = requestRequest c.initChannel() c.server = s @@ -340,7 +348,7 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - s.SetupEventLoop(conn, r.RemoteAddr, r.Header) + s.SetupEventLoop(conn, r.RemoteAddr, r.Header, *r) s.tr.Serve(w, r) }