Exemple #1
0
// ServerWs handles webocket requests from the peer.
func ServeWs(w http.ResponseWriter, r *http.Request) {
	go h.run() //Starting hub, this is different than in example
	if r.Method != "GET" {
		http.Error(w, "Method not allowed", 405)
		return
	}
	ws, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		if _, ok := err.(websocket.HandshakeError); !ok {
			log.Println(err)
		}
		return
	}
	c := &connection{send: make(chan []byte, 256), ws: ws}
	cookie, err := r.Cookie("session")
	if err != nil {
		// fmt.Printf("Cookie doesn't exist: %v\n", err)
	} else {
		// fmt.Printf("\nQuerying session for id: %s\n", cookie.Value)
		if session.SessionExist(cookie.Value) {
			// fmt.Printf("Session set on websocket connection: %s\n", cookie.Value)
			c.sessionId = cookie.Value
		} else {
			// fmt.Printf("Session doesn't exist: %s\n", cookie.Value)
			// fmt.Printf("Session store: %+v\n", session.SessionStore())
		}
	}
	h.register <- c

	// log.Printf("Client connected: %+v\n", c)
	go c.writePump()
	c.readPump()
	// fmt.Fprintf(w, "connection success")
}
Exemple #2
0
func ResourceHandler(w http.ResponseWriter, r *http.Request, dropsResponse *protocol.DropsResponse) *protocol.DropsResponse {
	// log.Printf("Resource handler: %s - %s\n", r.Method, r.URL.Path)
	var response string

	if w != nil && dropsResponse.Dom != nil {
		var sessionId string
		sessionCookie, err := r.Cookie("session")
		// fmt.Printf("sessionCookie: %+v\n", pretty.Formatter(sessionCookie))
		if err != nil {
			// fmt.Printf("Error fetching cookie %v\n", err)
			//No cookie found
			sessionId = session.CreateSession("")
			sessionCookie = &http.Cookie{Name: "session", Value: sessionId, Path: "/"}
			// fmt.Printf("Created Cookie: %s: %+v\n", sessionCookie.Name, sessionCookie.Value)
		} else {
			// log.Printf("Cookie: %s: %+v\n", sessionCookie.Name, sessionCookie.Value)
			sessionId = sessionCookie.Value
			if !session.SessionExist(sessionId) {
				sessionId = session.CreateSession(sessionId)
			}
			sessionCookie = &http.Cookie{Name: "session", Value: sessionId, Path: "/"}
			// sessionCookie.Value = sessionId
		}
		// fmt.Printf("sessionId: %s\n", sessionId)
		session.SetSessionActiveDOM(sessionId, dropsResponse.Dom)
		// fmt.Printf("\ndom set as active: %+v\n", pretty.Formatter(dom.Id))

		// ActiveDOM = dom
		buffer := element.Render(&dropsResponse.Dom.View)
		response = buffer.String()
		// cookie := &http.Cookie{Name: "session", Value: sessionId}
		// http.Set
		http.SetCookie(w, sessionCookie)

		fmt.Fprint(w, response)

		return nil
	}
	return dropsResponse
}