Ejemplo n.º 1
0
func TestUnallowedMethods(t *testing.T) {
	m := martini.Classic()

	recorder := httptest.NewRecorder()
	req, err := http.NewRequest("POST", "/test", strings.NewReader(""))

	if err != nil {
		t.Error(err)
	}

	m.Any("/test", Messages(), func() int {
		return http.StatusOK
	})

	m.ServeHTTP(recorder, req)
	expectStatusCode(t, http.StatusMethodNotAllowed, recorder.Code)
}
Ejemplo n.º 2
0
func main() {
	m := martini.Classic()

	// Use Renderer
	m.Use(render.Renderer(render.Options{
		Layout: "layout",
	}))

	// Create the chat
	chat = newChat()

	// Index
	m.Get("/", func(r render.Render) {
		r.HTML(200, "index", "")
	})

	// render the room
	m.Get("/rooms/:name", func(r render.Render, params martini.Params) {
		r.HTML(200, "room", map[string]map[string]string{"room": map[string]string{"name": params["name"]}})
	})

	// This is the sockets connection for the room, it is a json mapping to sockets.
	m.Get("/sockets/rooms/:name", sockets.JSON(Message{}), func(params martini.Params, receiver <-chan *Message, sender chan<- *Message, done <-chan bool, disconnect chan<- int, err <-chan error) {
		client := &Client{receiver, sender, done, err, disconnect}
		r := chat.getRoom(params["name"])
		r.appendClient(client)

		// A single select can be used to do all the messaging
		for {
			select {
			case err := <-client.err:
				client.out <- &Message{"", "There has been an error with your connection: " + err.Error()}
			case msg := <-client.in:
				r.messageOtherClients(client, msg)
			case <-client.done:
				r.removeClient(client)
				return
			}
		}
	})

	m.Run()
}
Ejemplo n.º 3
0
func startServer() {
	m := martini.Classic()

	m.Get(recvPath, JSON(Message{}), func(context martini.Context, receiver <-chan *Message, done <-chan bool) int {
		for {
			select {
			case msg := <-receiver:
				recvMessages = append(recvMessages, msg)
			case <-done:
				recvDone = true
				return http.StatusOK
			}
		}

		return http.StatusOK
	})

	m.Get(sendPath, JSON(Message{}), func(context martini.Context, sender chan<- *Message, done <-chan bool, disconnect chan<- int) int {
		ticker := time.NewTicker(1 * time.Millisecond)
		bomb := time.After(4 * time.Millisecond)

		for {
			select {
			case <-ticker.C:
				sender <- &Message{"Hello World"}
			case <-done:
				ticker.Stop()
				sendDone = true
				return http.StatusOK
			case <-bomb:
				disconnect <- websocket.CloseGoingAway
				return http.StatusOK
			}
		}

		return http.StatusOK
	})

	m.Get(recvStringsPath, Messages(), func(context martini.Context, receiver <-chan string, done <-chan bool) int {
		for {
			select {
			case msg := <-receiver:
				recvStrings = append(recvStrings, msg)
			case <-done:
				recvStringsDone = true
				return http.StatusOK
			}
		}

		return http.StatusOK
	})

	m.Get(sendStringsPath, Messages(), func(context martini.Context, sender chan<- string, done <-chan bool, disconnect chan<- int) int {
		ticker := time.NewTicker(1 * time.Millisecond)
		bomb := time.After(4 * time.Millisecond)

		for {
			select {
			case <-ticker.C:
				sender <- "Hello World"
			case <-done:
				ticker.Stop()
				sendStringsDone = true
				return http.StatusOK
			case <-bomb:
				disconnect <- websocket.CloseGoingAway

				return http.StatusOK
			}
		}

		return http.StatusOK
	})

	go m.Run()
	time.Sleep(5 * time.Millisecond)
}