Permalink
Fetching contributors…
Cannot retrieve contributors at this time
48 lines (38 sloc) 1007 Bytes
package controllers
import (
"github.com/revel/revel"
"github.com/revel/examples/chat/app/chatroom"
)
type LongPolling struct {
*revel.Controller
}
func (c LongPolling) Room(user string) revel.Result {
chatroom.Join(user)
return c.Render(user)
}
func (c LongPolling) Say(user, message string) revel.Result {
chatroom.Say(user, message)
return nil
}
func (c LongPolling) WaitMessages(lastReceived int) revel.Result {
subscription := chatroom.Subscribe()
defer subscription.Cancel()
// See if anything is new in the archive.
var events []chatroom.Event
for _, event := range subscription.Archive {
if event.Timestamp > lastReceived {
events = append(events, event)
}
}
// If we found one, grand.
if len(events) > 0 {
return c.RenderJSON(events)
}
// Else, wait for something new.
event := <-subscription.New
return c.RenderJSON([]chatroom.Event{event})
}
func (c LongPolling) Leave(user string) revel.Result {
chatroom.Leave(user)
return c.Redirect(Application.Index)
}