// Accepts a http connection & request pair. It upgrades the connection and calls
// proceed if succesfull.
//
// TODO: Remove the ugly channels and timeouts. They should not be needed!
func (s *websocketSocket) accept(w http.ResponseWriter, req *http.Request, proceed func()) (err os.Error) {
	if s.connected {
		return ErrConnected
	}

	f := func(ws *websocket.Conn) {
		err = nil
		ws.SetReadTimeout(s.t.rtimeout)
		ws.SetWriteTimeout(s.t.wtimeout)
		s.connected = true
		s.ws = ws
		s.close = make(chan byte)
		defer close(s.close)

		proceed()

		// must block until closed
		<-s.close
	}

	err = errWebsocketHandshake
	if _, ok := req.Header["Sec-Websocket-Key1"]; ok {
		websocket.Handler(f).ServeHTTP(w, req)
	} else {
		websocket.Draft75Handler(f).ServeHTTP(w, req)
	}

	return
}
Esempio n. 2
0
func webSocketProtocolSwitch(c http.ResponseWriter, req *http.Request) {
	// Handle old and new versions of protocol.
	if _, found := req.Header["Sec-Websocket-Key1"]; found {
		websocket.Handler(clientHandler).ServeHTTP(c, req)
	} else {
		websocket.Draft75Handler(clientHandler).ServeHTTP(c, req)
	}
}