-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
55 lines (45 loc) · 1.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"fmt"
"log"
"net/http"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
var (
server = newServer()
)
func hello(c echo.Context) error {
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
conn, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
log.Println(err)
return nil
}
roomID := c.QueryParam("room_id")
fmt.Printf("room id: %+v\n", roomID)
if roomID != "" {
server.addClientToRoomByID(roomID, conn)
}
return nil
}
func processData(c echo.Context) error {
fmt.Printf("rooms count: %+v\n", server.roomsCount())
roomID := c.QueryParam("room_id")
msg := c.QueryParam("msg")
fmt.Printf("msg get param: %s\n", msg)
//TODO: add error msg if room is empty
if roomID != "" {
server.broadcastToRoomByID(roomID, msg)
}
return c.String(http.StatusOK, "")
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Static("/", "./public")
e.GET("/ws", hello)
e.GET("/data", processData)
e.Logger.Fatal(e.Start(":1323"))
}