コード例 #1
0
ファイル: room.go プロジェクト: ZeeeL/chatex
func (self Room) History(roomId int64) revel.Result {
	var room = models.GetRoom(roomId)
	if room == nil {
		return self.NotFound("Room was not found")
	}
	return self.RenderJson(room.GetMessages())
}
コード例 #2
0
ファイル: room.go プロジェクト: ZeeeL/chatex
func (self *Room) Show(roomId int64) revel.Result {
	var room = models.GetRoom(roomId)
	if room == nil {
		return self.NotFound("Room was not found")
	}
	return self.Render(room)
}
コード例 #3
0
ファイル: room.go プロジェクト: ZeeeL/chatex
func (self Room) Publish(roomId int64, text string, image_url string) revel.Result {
	self.Validation.Required(text)
	if self.Validation.HasErrors() {
		return self.RenderJson("text required")
	}
	var room = models.GetRoom(roomId)
	if room == nil {
		return self.NotFound("Room was not found")
	}
	var msg = models.Message{
		Text:     text,
		ImageUrl: image_url,
	}
	room.AddMessage(msg)
	return self.RenderJson(true)
}
コード例 #4
0
ファイル: room.go プロジェクト: ZeeeL/chatex
func (self Room) Subscribe(ws *websocket.Conn, roomId int64) revel.Result {
	var room = models.GetRoom(roomId)
	if room == nil {
		return self.NotFound("Room was not found")
	}

	sub := chat.Join(fmt.Sprintf("Anonymous[%s]", ws.Request().RemoteAddr), roomId)

	defer chat.Cancel(sub)
	defer ws.Close()

	for {
		event := <-sub.Channel
		revel.INFO.Printf("%s: Received chat event %s", sub.String(), event.String())

		if event.Type == chat.EVENT_MSG {
			revel.INFO.Printf("Publishing message to room #%d\n", event.RoomId)
			if websocket.JSON.Send(ws, event) != nil {
				revel.WARN.Println("Client disconnected")
				break
			}
		} else if event.Type == chat.EVENT_PING {
			if websocket.JSON.Send(ws, event) != nil {
				revel.WARN.Println("Client disconnected")
				break
			}
		} else if event.Type == chat.EVENT_NEW_USER {
			websocket.JSON.Send(ws, event)
		} else if event.Type == chat.EVENT_USER_DISCONNECTED {
			websocket.JSON.Send(ws, event)
		} else {
			revel.ERROR.Fatal("Unknown event", event)
		}

	}

	return nil
}