コード例 #1
0
ファイル: websocket.go プロジェクト: Joinhack/peony
//@Mapper("/websocket/room/socket", method="WS")
func (c WebSocket) RoomSocket(user string, ws *websocket.Conn) {
	// 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 websocket.JSON.Send(ws, &event) != nil {
			// They disconnected
			return
		}
	}

	// 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 := websocket.Message.Receive(ws, &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 websocket.JSON.Send(ws, &event) != nil {
				// They disconnected.
				return
			}
		case msg, ok := <-newMessages:
			// If the channel is closed, they disconnected.
			if !ok {
				return
			}

			// Otherwise, say something.
			chatroom.Say(user, msg)
		}
	}
	return
}
コード例 #2
0
ファイル: refresh.go プロジェクト: Joinhack/peony
//@Mapper("room/leave")
func (c Refresh) Leave(user string) Renderer {
	chatroom.Leave(user)
	return Redirect(Application.Index)
}
コード例 #3
0
ファイル: longpolling.go プロジェクト: Joinhack/peony
//@Mapper("/longpolling/room/leave")
func (c LongPolling) Leave(user string) Renderer {
	chatroom.Leave(user)
	return Redirect(Application.Index)
}