Example #1
0
func (c Application) Api(username string, requestType string, requestCount int, requestPlace string) revel.Result {
	var user models.User
	if err := c.Txn.SelectOne(&user, "SELECT * FROM User where alias=?", username); err != nil {
		return c.RenderText("We couldn't find User Alias")
	} else {
		request := models.Requests{
			Alias:        username,
			RequestType:  requestType,
			RequestCount: requestCount,
			RequestTime:  time.Now(),
			IsActive:     true,
			User:         &user,
			RequestPlace: requestPlace,
		}

		request.Validate(c.Validation)

		if c.Validation.HasErrors() {
			return c.RenderText("Error loadings a requested user")
		} else {
			if err := c.Txn.Insert(&request); err != nil {
				return c.RenderText(
					"Error inserting record into database!")
			} else {
				chatroom.SendRequest(&request)
				return c.RenderJson(request)
			}
		}
	}
}
Example #2
0
func (c WebSocket) RoomSocket(ws *websocket.Conn) revel.Result {
	// Join the room.
	subscription := chatroom.Subscribe()
	defer subscription.Cancel()

	// In order to select between websocket messages and subscription events, we
	// need to stuff websocket events into a channel.
	newMessages := make(chan models.Requests)
	go func() {
		var msg models.Requests
		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 nil
			}
		case msg, ok := <-newMessages:
			// If the channel is closed, they disconnected.
			if !ok {
				return nil
			}
			// Otherwise, say something.
			chatroom.SendRequest(&msg)
		}
	}
	return nil
}