예제 #1
0
파일: dblistener.go 프로젝트: ZeeeL/chatex
func waitForNotification(l *pq.Listener) {
	for {
		select {
		case notify := <-l.Notify:

			payload := strings.SplitN(notify.Extra, "|", 3)

			id, err := strconv.ParseInt(payload[0], 10, 64)
			if err != nil {
				panic(err)
			}
			var roomId int64
			roomId, err = strconv.ParseInt(payload[1], 10, 64)
			if err != nil {
				panic(err)
			}

			msg := models.GetMessage(id)

			revel.INFO.Printf("received notification with payload: '%d' '%d' '%s' '%s'\n", msg.Id, msg.RoomId, msg.Text, msg.ImageUrl)
			Publish(EVENT_MSG, int64(roomId), *msg)

		case <-time.After(200 * time.Millisecond):
			go func() {
				if err := l.Ping(); err != nil {
					panic(err)
				}
			}()
		}
	}
}
예제 #2
0
func waitForNotification(l *pq.Listener) {
	for {
		select {
		case n := <-l.Notify:
			fmt.Println("Received data from channel [", n.Channel, "] :")
			// Prepare notification payload for pretty print
			var prettyJSON bytes.Buffer
			err := json.Indent(&prettyJSON, []byte(n.Extra), "", "\t")
			if err != nil {
				fmt.Println("Error processing JSON: ", err)
				return
			}
			fmt.Println(string(prettyJSON.Bytes()))
			return
		case <-time.After(90 * time.Second):
			fmt.Println("Received no events for 90 seconds, checking connection")
			go func() {
				l.Ping()
			}()
			return
		}
	}
}