MCPcopy Create free account
hub / github.com/koding/kite / DialXHR

Function DialXHR

sockjsclient/xhr.go:49–84  ·  view source on GitHub ↗

DialXHR establishes a SockJS session over a XHR connection. Requires cfg.XHR to be a valid client.

(uri string, cfg *config.Config)

Source from the content-addressed store, hash-verified

47//
48// Requires cfg.XHR to be a valid client.
49func DialXHR(uri string, cfg *config.Config) (*XHRSession, error) {
50 // following /server_id/session_id should always be the same for every session
51 serverID := threeDigits()
52 sessionID := utils.RandomString(20)
53 sessionURL := uri + "/" + serverID + "/" + sessionID
54
55 // start the initial session handshake
56 sessionResp, err := cfg.XHR.Post(sessionURL+"/xhr", "text/plain", nil)
57 if err != nil {
58 return nil, err
59 }
60 defer sessionResp.Body.Close()
61
62 if sessionResp.StatusCode != http.StatusOK {
63 return nil, fmt.Errorf("Starting new session failed. Want: %d Got: %d",
64 http.StatusOK, sessionResp.StatusCode)
65 }
66
67 frame, err := newFrameReader(sessionResp.Body, cfg.Timeout).ReadByte()
68 if err != nil {
69 return nil, err
70 }
71
72 if frame != 'o' {
73 return nil, fmt.Errorf("can't start session, invalid frame: %s", string(frame))
74 }
75
76 return &XHRSession{
77 client: cfg.XHR,
78 timeout: cfg.Timeout,
79 sessionID: sessionID,
80 sessionURL: sessionURL,
81 state: sockjs.SessionActive,
82 abort: make(chan struct{}, 1),
83 }, nil
84}
85
86// NewXHRSession returns a new XHRSession, a SockJS client which supports xhr-polling:
87//

Callers 1

NewXHRSessionFunction · 0.85

Calls 4

threeDigitsFunction · 0.85
newFrameReaderFunction · 0.85
ReadByteMethod · 0.80
CloseMethod · 0.45

Tested by

no test coverage detected