/
realtime.go
103 lines (94 loc) · 3.1 KB
/
realtime.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package samplegame
import (
"encoding/base64"
"fmt"
"github.com/hayabusa-cloud/hybs-server/application/common"
"time"
hybs "github.com/hayabusa-cloud/hayabusa"
"github.com/hayabusa-cloud/hybs-server/application/middleware/realtime"
"github.com/labstack/gommon/random"
)
const (
rtEventCodeSampleGameSum = 0x8200
)
// a sample API: calculate the sum of a + b
func v1SampleGameRTTest(ctx hybs.RealtimeCtx) {
var a, b int16
ctx.ReadInt16(&a).ReadInt16(&b)
var out = ctx.OutPacket()
out.SetEventCode(rtEventCodeSampleGameSum)
out.WriteInt16(a + b)
ctx.Response(out) // response to requested user
// ctx.Send(destination, out) // send message to specified user
// ctx.BroadcastRoom(out) // broadcast to all users in the same room
// ctx.BroadcastServer(out) // broadcast to all online users
// can also get hayabusa id like this:
// ctx.UserID()
}
// a test middleware: give user permission
func rtMiddlewareTestAuthorization(h hybs.RealtimeHandler) hybs.RealtimeHandler {
return func(ctx hybs.RealtimeCtx) {
// check authorization
// check user information from db/cache etc.
// ...
// end check authorization
ctx.GivePermission(common.UserPermissionNormal)
h(ctx)
}
}
// a sample middleware
func rtMiddlewareSampleGameQPS(h hybs.RealtimeHandler) hybs.RealtimeHandler {
// here is concurrency safe
var (
requestCounter = 0
requestCountedAt = hybs.TimeNil()
)
return func(ctx hybs.RealtimeCtx) {
// here is concurrency safe
h(ctx)
// statistics query per second
if requestCountedAt.IsZero() {
requestCountedAt = ctx.Now()
}
requestCounter++
if ctx.Now().Unix() != requestCountedAt.Unix() {
fmt.Println("query per second:", requestCounter)
requestCounter = 0
}
requestCountedAt = ctx.Now()
}
}
// generate temptation access token
func v1RealtimeAccessTokenPost(ctx hybs.Ctx) {
var appID = ctx.FormString("app_id")
if appID == "" {
ctx.SysLogf("empty app_id")
ctx.StatusBadRequest()
return
}
// straightly generate onetime user account of playground env
// implement customized accounts&authorization system in your own app
var (
onetimeUserID = []byte(random.String(8, random.Alphanumeric))
onetimePassword = []byte(random.String(15, random.Alphanumeric))
)
var src = []byte(fmt.Sprintf("%s#%s#%s", appID, onetimeUserID, onetimePassword))
var token = []byte(base64.StdEncoding.EncodeToString(src))
var accessToken = &realtime.AccessToken{
AppID: appID,
HayabusaID: onetimeUserID,
Mux: "playground",
Token: token,
ValidUntil: ctx.Now().Add(time.Hour * 24 * 3),
}
var cacheKey = fmt.Sprintf("/Realtime/Tokens/%s", accessToken.Token)
ctx.Cache().Set(cacheKey, accessToken, time.Hour*24*3)
ctx.SetResponseValue("token", accessToken.Token)
ctx.SetResponseValue("expireUntil", accessToken.ValidUntil)
}
func init() {
hybs.RegisterRealtimeHandler("RTSampleGameTestV1", v1SampleGameRTTest)
hybs.RegisterRealtimeMiddleware("RTSampleGameQPS", rtMiddlewareSampleGameQPS)
hybs.RegisterRealtimeMiddleware("RTSampleGameTestAuthorization", rtMiddlewareTestAuthorization)
hybs.RegisterService("RealtimeCreateAccessTokenV1", v1RealtimeAccessTokenPost)
}