Ejemplo n.º 1
0
//Handles event and creates DOM patch
func (c *connection) handleExecute(handle router.Handle, paramsFromRequest router.Params) ([]byte, error) {
	dropsResponse := &protocol.DropsResponse{}
	activeDOM := session.GetSessionActiveDOM(c.sessionId)
	// fmt.Printf("\nGetting activeDOM on websocket connection: %+v\n", pretty.Formatter(activeDOM))
	if handle != nil {
		sessionParam := router.Param{Key: "session", Value: c.sessionId}

		paramsFromRequest = append(paramsFromRequest, sessionParam)
		// fmt.Printf("Routing success: %v\n", paramsFromRequest)

		dropsResponse = handle(nil, nil, paramsFromRequest)
		dropsResponse.ActiveDom = activeDOM
		// PrintDOM(ActiveDOM, "1")

		// fmt.Printf("ActiveDOM: %+v\n", activeDOM)
		// fmt.Printf("New DOM: %+v\n", dom)
		// fmt.Printf("Active dom is the same to new DOM: %v\n", *ActiveDOM == *dom)
		// PrintDOM(dom, "2")
	} else {
		log.Println("Routing failure, no handler")

		dropsResponse.Dom = activeDOM
	}
	var message []byte
	// var err error
	session.SetSessionActiveDOM(c.sessionId, dropsResponse.Dom)

	message = protocol.GenerateMessage(dropsResponse)

	fmt.Printf("Drops response: %+v\n", string(message))
	return message, nil

}
Ejemplo n.º 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
}