Ejemplo n.º 1
0
func handleViewer(conn *websocket.Conn) {
	conn.SetReadDeadline(time.Now().Add(10 * time.Second))
	var viewId string
	if err := websocket.Message.Receive(conn, &viewId); err != nil {
		return
	}

	slideId := index.getSlideId(viewId)
	if slideId == "" {
		return
	}

	listener := &slideListener{ch: make(chan int)}
	registry.addListener(slideId, listener)

	for {
		slide := listener.get(1 * time.Minute)
		if slide != 0 {
			conn.SetDeadline(time.Now().Add(10 * time.Second))
			if err := websocket.Message.Send(conn, fmt.Sprintf("%d", slide)); err != nil {
				registry.removeListener(slideId, listener)
				return
			}

			continue
		}

		if err := ping(conn); err != nil {
			registry.removeListener(slideId, listener)
			return
		}
	}
}
Ejemplo n.º 2
0
func ping(conn *websocket.Conn) error {
	conn.SetDeadline(time.Now().Add(10 * time.Second))
	if err := websocket.Message.Send(conn, "ping"); err != nil {
		return err
	}

	var pong string
	if err := websocket.Message.Receive(conn, &pong); err != nil {
		return err
	}

	return nil
}
Ejemplo n.º 3
0
func handlePresenter(conn *websocket.Conn) {
	conn.SetReadDeadline(time.Now().Add(10 * time.Second))
	var slideIdParam string
	if err := websocket.Message.Receive(conn, &slideIdParam); err != nil {
		return
	}

	slideId, _ := index.getIdPair(slideIdParam)
	if slideId == "" {
		return
	}

	for {
		var slide string

		conn.SetDeadline(time.Now().Add(15 * time.Minute))
		if err := websocket.Message.Receive(conn, &slide); err != nil {
			return
		}

		curSlide, _ := strconv.Atoi(slide)
		registry.setSlide(slideId, curSlide)
	}
}