Esempio n. 1
0
//保持心跳,维持通信
func (c *Client) Keeplink() {
	ticker := time.NewTicker(PONGWAIT)
	c.ws.SetReadDeadline(time.Now().Add(WRITEWAIT))
	c.ws.SetPongHandler(func(string) error {
		c.ws.SetReadDeadline(time.Now().Add(WRITEWAIT))
		return nil
	})
	defer func() {
		ticker.Stop()
		if r := recover(); r != nil {
			model.CheckRecover()
		}
		close(c.shutdown)
	}()
	for {
		select {
		case <-ticker.C:
			//发送一个ping信号,维持通信
			if err := c.ws.WriteMessage(websocket.PingMessage, []byte{}); err != nil {
				return
			}
		case flag := <-c.shutdown:
			if flag == true {
				Println("shuting down")
				return
			}
		}
	}
}
Esempio n. 2
0
func (c *Client) readPump() {
	for {
		//其是可以用来向其他的连接推送广播的
		_, _, err := c.ws.ReadMessage()
		if err != nil {
			break
		}
		//h.broadcast <- string(msg)
	}
	defer func() {
		if r := recover(); r != nil {
			model.CheckRecover()
		}
		c.ws.Close()
		h.unregister <- c
		c.shutdown <- true
	}()
}
Esempio n. 3
0
File: main.go Progetto: unasm/gopush
/*
func unescaped(x string) interface{} { return template.HTML(x) }
func renderTemplate(w http.ResponseWriter, tmpl string, view *Page) {
	t := template.New("")
	t = t.Funcs(template.FuncMap{"unescaped": unescaped})
	t, err := t.ParseFiles("view.html", "edit.html")
	err = t.ExecuteTemplate(w, tmpl+".html", view)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
*/
func Index(w http.ResponseWriter, r *http.Request) {

	//Println(r.Header["Sec-Websocket-Version"][0])
	if r.Proto != "HTTP/1.1" || r.Header["Sec-Websocket-Version"][0] != "13" {
		Fprintf(w, "request proto is not permit")
		return
	}
	defer func() {
		if r := recover(); r != nil {
			model.CheckRecover()
		}
	}()
	var upgrader = websocket.Upgrader{
		ReadBufferSize: 1024, WriteBufferSize: 1024,
		//HandshakeTimeout: HANDSHAKE,
		//握手时间超过3s超时
		//不再检查请求源
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}
	r.ParseForm()
	model.Form = r.Form
	if len(r.Form["pid"]) == 0 {
		Println("No pid")
		return
	}
	if model.Isint("pid", r.Form["pid"][0]) == false {
		Println("aid is not int")
		return
	}

	pid, err := strconv.Atoi(r.Form["pid"][0])
	model.CheckErr(err, "转换失败")

	conn, err := upgrader.Upgrade(w, r, nil)
	model.CheckErr(err, "升级失败")
	if err != nil {
		Fprintf(w, "upgrader faied")
		return
	}
	wb.Send("one more")
	wb.StartNew(pid, "", conn)
}
Esempio n. 4
0
File: main.go Progetto: unasm/gopush
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())
	//cc := config.Host{}
	config.Init()
	wb.Init()
	inspect.Inspect()
	defer func() {
		if r := recover(); r != nil {
			model.CheckRecover()
		}
	}()
	//go model.Copy()
	http.HandleFunc("/chat/", Index)
	http.HandleFunc("/inspect/", Home)
	//Println("step")
	//http.HandleFunc("/chat/", Home)
	//go model.Run()
	//go h.run()
	//wb.StartListenBroad()
	if err := http.ListenAndServe(":8010", nil); err != nil {
		log.Fatal("Listen and add serve error ", err)
	}
}