Permalink
Fetching contributors…
Cannot retrieve contributors at this time
74 lines (61 sloc) 1.39 KB
package controllers
import (
"github.com/revel/revel"
"github.com/revel/examples/chat/app/chatroom"
)
type WebSocket struct {
*revel.Controller
}
func (c WebSocket) Room(user string) revel.Result {
return c.Render(user)
}
func (c WebSocket) RoomSocket(user string, ws revel.ServerWebSocket) revel.Result {
// Make sure the websocket is valid.
if ws == nil {
return nil
}
// Join the room.
subscription := chatroom.Subscribe()
defer subscription.Cancel()
chatroom.Join(user)
defer chatroom.Leave(user)
// Send down the archive.
for _, event := range subscription.Archive {
if ws.MessageSendJSON(&event) != nil {
// They disconnected
return nil
}
}
// In order to select between websocket messages and subscription events, we
// need to stuff websocket events into a channel.
newMessages := make(chan string)
go func() {
var msg string
for {
err := ws.MessageReceiveJSON(&msg)
if err != nil {
close(newMessages)
return
}
newMessages <- msg
}
}()
// Now listen for new events from either the websocket or the chatroom.
for {
select {
case event := <-subscription.New:
if ws.MessageSendJSON(&event) != nil {
// They disconnected.
return nil
}
case msg, ok := <-newMessages:
// If the channel is closed, they disconnected.
if !ok {
return nil
}
// Otherwise, say something.
chatroom.Say(user, msg)
}
}
return nil
}