示例#1
0
func Recommand(filePath string, people []*Person) (err error) {
	dict := make(map[string]interface{}) //people: []*Person, product: Product
	var product *Product
	if people != nil && len(people) > 0 {
		switch len(people) {
		case 1:
			product, err = recommandFor1(people[0])
		case 2:
			product, err = recommandFor2(people)
		default:
			product, err = recommandForMulti(people)
		}
		if err != nil {
			utils.Errorf("Error recommand: %s", err.Error())
		} else {
			utils.Debugf("Product: %s", product.Name)
		}
	}
	dict["file"] = filePath
	dict["people"] = people
	dict["product"] = product
	if WebSocketConn != nil {
		if err = WebSocketConn.WriteJSON(dict); err != nil {
			utils.Errorf("Error write json to websocket: %s", err.Error())
		}
	} else {
		utils.Debugf("Websocket is nil")
	}
	return
}
示例#2
0
func Handle(w http.ResponseWriter, r *http.Request) {
	upgrader := websocket.Upgrader{
		CheckOrigin: func(req *http.Request) bool {
			if req.Host == "127.0.0.1:9090" {
				return true
			} else {
				return false
			}
		},
	}
	var err error
	WebSocketConn, err = upgrader.Upgrade(w, r, nil)
	if err != nil {
		utils.Errorf("Error upgrade http: %s", err.Error())
		return
	}
	for {
		mt, bytes, err := WebSocketConn.ReadMessage()
		if err != nil {
			utils.Errorf("Error read message: %v", err)
			WebSocketConn.Close()
			WebSocketConn = nil
			return
		}
		utils.Debugf("Client msg received: %d, %s", mt, string(bytes))
	}
}
示例#3
0
func StartServer() (err error) {
	fmt.Println("Websocket server start now...")
	http.HandleFunc("/face", Handle)
	if err = http.ListenAndServe(":9090", nil); err != nil {
		utils.Errorf("Error serve http: %s", err.Error())
	}
	return
}
示例#4
0
文件: sdk.go 项目: m4ker/uberwar
func do(accessToken string, apiUrl string, method string) (bytes []byte, err error) {
	client := &http.Client{}
	r, err := http.NewRequest(method, apiUrl, nil)
	if err != nil {
		utils.Errorf("Error new http request: %s", err.Error())
		return
	}
	r.Header.Add("Authorization", " Bearer "+accessToken)
	response, err := client.Do(r)
	defer response.Body.Close()
	bytes, err = ioutil.ReadAll(response.Body)
	return
}
示例#5
0
func handleOauthRedirect(w http.ResponseWriter, r *http.Request) {
	code := r.FormValue("code")
	utils.Debugf("Oauth redirect, code: %s", code)
	resp, err := http.PostForm("https://login.uber.com.cn/oauth/v2/token", url.Values{
		"client_secret": {conf.Get("uber_client_secret")},
		"client_id":     {"DV6gJE19BMU6LIh0jJYAvtFyel8rpCfe"},
		"grant_type":    {"authorization_code"},
		"redirect_uri":  {"https://tripwar.laoyou.mobi/oauth_redirect"},
		"code":          {code},
	})
	if err != nil {
		utils.Errorf("Error post to oauth: %s", err.Error())
		w.Write([]byte("500"))
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	result := &OauthResult{}
	if err = json.Unmarshal(body, result); err != nil {
		utils.Errorf("Error unmarshal oauth result: %s", err.Error())
		w.Write([]byte("500"))
		return
	}
	utils.Debugf("access token: %s", result.AccessToken)
	sid := uniuri.NewLen(10)
	sids = append(sids, sid)
	utils.SetCache(sid+"_access_token", result.AccessToken, 86400)
	go func(token string) {
		profile, err := uber.GetUserProfile(token)
		if err != nil {
			utils.Errorf("Error get user profile: %s", err.Error())
			return
		}
		user := &models.User{
			Id:              profile.Uuid,
			Name:            profile.FirstName + " " + profile.LastName,
			UberOpenId:      profile.Uuid,
			Faction:         int64(rand.Intn(2)),
			TotalScore:      10000,
			Score:           0,
			Mana:            10,
			Hp:              10,
			Lng:             116.310228,
			Lat:             39.979233,
			LocationBearing: 33,
		}
		utils.Debugf("user: %v", user)
		users[sid] = user
	}(result.AccessToken)
	go func(sid string) {
		timer := time.NewTicker(30 * time.Second)
		for {
			select {
			case <-timer.C:
				token := utils.GetCache(sid + "_access_token")
				current, _ := uber.GetCurrent(token)
				utils.Debugf("current: %v", current)
				sidCurrent[sid] = current
			}
		}
	}(sid)
	http.Redirect(w, r, "http://tripwar.laoyou.mobi/html/index.html?sid="+sid, 302)
}