/
websocket.go
83 lines (67 loc) · 2.02 KB
/
websocket.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package controllers
// WebSocket controller
import (
"github.com/gorilla/websocket"
"github.com/stef-k/gosimple/models"
"github.com/astaxie/beego"
"net/http"
"fmt"
)
type WebsocketController struct {
beego.Controller
}
// Live a WebSocket controller - upgrader
func (wc *WebsocketController) Get() {
websocketLimits, _ := beego.AppConfig.Int("websocket::limit")
// access if any parameters are passed
// along with the request to connect using
// the websockets protocol
roomName := wc.Ctx.Input.Param("room")
if roomName == "" {
roomName = "lobby"
}
// upgrade the connection
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
// Upgrade
ws, err := upgrader.Upgrade(wc.Ctx.ResponseWriter, wc.Ctx.Request, nil)
if _, ok := err.(websocket.HandshakeError); ok {
panic(err)
return
}
currentConnections := models.GetAllClients()
// do not connect the client if we have reached server's limits
if currentConnections >= websocketLimits {
beego.Info("Server reached its limit, aborting connection")
return
}
// this will probably print 127.0.0.1:somePort
beego.Info("got new connection with IP: ", wc.Ctx.Request.RemoteAddr)
// create a new client and do something with it
client := models.NewClient(ws, roomName)
// find room else create a new one
index, room := models.FindRoom(roomName)
if index == -1 {
room := models.NewRoom(roomName)
room.AddClient(client)
models.AddRoom(room)
} else {
room.AddClient(client)
}
client.SendMessage(models.NewMessage(map[string]string{
"1" : "Hello websocket client",
"2" : "You are now connected",
}))
// client side debug, on every client connection, broadcast server status
models.PoolBroadcast(models.NewMessage(map[string]string{
"connectedClients": fmt.Sprintf("%v", models.GetAllClients()),
"numberOfRooms": fmt.Sprintf("%v", models.GetNumberOfRooms()),
}))
// serve something so beego does not complain for missing template...
wc.ServeJSON()
}