Skip to content

Commit

Permalink
added withLength() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfelian committed Apr 26, 2018
1 parent a7d2c94 commit 7582d30
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
11 changes: 7 additions & 4 deletions transport/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"time"

"fmt"
"github.com/mtfelian/golang-socketio/logging"
"github.com/mtfelian/golang-socketio/protocol"
)
Expand All @@ -32,6 +32,9 @@ var (
errWriteMessageTimeout = errors.New("timeout waiting for write")
)

// withLength returns s as a message with length
func withLength(m string) string { return fmt.Sprintf("%d:%s", len(m), m) }

// PollingTransportParams represents XHR polling transport params
type PollingTransportParams struct {
Headers http.Header
Expand Down Expand Up @@ -212,8 +215,8 @@ func (polling *PollingConnection) PollingWriter(w http.ResponseWriter, r *http.R
polling.errors <- noError
case message := <-polling.eventsOutC:
logging.Log().Debug("PollingTransport.PollingWriter() prepares to write message:", message)
message = strconv.Itoa(len(message)) + ":" + message
if message == protocol.MessageClose+":"+protocol.MessageBlank {
message = withLength(message)
if message == withLength(protocol.MessageBlank) {
logging.Log().Debug("PollingTransport.PollingWriter() writing 1:6")

hj, ok := w.(http.Hijacker)
Expand All @@ -234,7 +237,7 @@ func (polling *PollingConnection) PollingWriter(w http.ResponseWriter, r *http.R
"Cache-Control: no-cache, private\r\n" +
"Content-Length: 3\r\n" +
"Date: Mon, 24 Nov 2016 10:21:21 GMT\r\n\r\n")
buffer.WriteString(protocol.MessageClose + ":" + protocol.MessageBlank)
buffer.WriteString(withLength(protocol.MessageBlank))
buffer.Flush()
logging.Log().Debug("PollingTransport.PollingWriter() hijack returns")
polling.errors <- noError
Expand Down
3 changes: 1 addition & 2 deletions transport/polling_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -54,7 +53,7 @@ func (polling *PollingClientConnection) GetMessage() (string, error) {

// WriteMessage performs a POST request to send a message to server
func (polling *PollingClientConnection) WriteMessage(message string) error {
msgToWrite := strconv.Itoa(len(message)) + ":" + message
msgToWrite := withLength(message)
logging.Log().Debug("PollingConnection.WriteMessage() fired, msgToWrite:", msgToWrite)
var jsonStr = []byte(msgToWrite)

Expand Down

0 comments on commit 7582d30

Please sign in to comment.