-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
48 lines (38 loc) · 1.26 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
package connection
import (
"context"
"net/http"
"github.com/dayaftereh/discover/server/utils"
"github.com/dayaftereh/discover/server/api"
wsconn "github.com/dayaftereh/discover/server/api/connection"
"github.com/pkg/errors"
)
func (connection *connectionRouter) websocket(ctx context.Context, response http.ResponseWriter, request *http.Request, variables map[string]string) error {
// get the sessionId
sessionID, err := api.SessionIdFromContext(ctx)
if err != nil {
return err
}
// get player for session
player := connection.backend.GetPlayerSession(sessionID)
// check if player login
if player == nil {
// do not allow befor login
return api.Forbidden(response)
}
// upgrade the connection to a websocket
conn, err := connection.upgrader.Upgrade(response, request, nil)
if err != nil {
return errors.Wrapf(err, "fail to upgrade incoming reguest to a stable websocket")
}
// genrade a random connection id
connectionID, err := utils.RandString(128)
if err != nil {
return errors.Wrapf(err, "unable to generated a random connection id")
}
// create a new connection
websocketConn := wsconn.NewConnection(connectionID, player, conn)
// notify dispatcher about new websocket connection
connection.dispatcher.EmitOpen(websocketConn)
return nil
}