1
1
// snippet.using
2
2
using PubnubApi ;
3
+ using PubnubApi . Unity ;
3
4
4
5
// snippet.end
6
+ using System ;
7
+ using System . Collections . Generic ;
8
+ using System . Threading . Tasks ;
9
+
10
+ using UnityEngine ;
5
11
6
12
class AccessManagerSample
7
13
{
@@ -17,46 +23,15 @@ static void PubnubInit()
17
23
PublishKey = "demo"
18
24
} ;
19
25
//Create a new PubNub instance
20
- Pubnub pubnub = new Pubnub ( pnConfiguration ) ;
21
-
22
- // snippet.end
23
- }
24
-
25
- static async Task BasicUsage ( )
26
- {
27
- // snippet.basic_usage
28
- try
29
- {
30
- //Perform token granting operation
31
- PNResult < PNAccessManagerTokenResult > grantTokenResponse = await pubnub . GrantToken ( )
32
- . TTL ( 15 )
33
- . AuthorizedUuid ( "my-authorized-uuid" )
34
- . Resources ( new PNTokenResources
35
- {
36
- Channels = new Dictionary < string , PNTokenAuthValues >
37
- {
38
- { "my-channel" , new PNTokenAuthValues { Read = true , Write = true } }
39
- }
40
- } )
41
- . ExecuteAsync ( ) ;
26
+ Pubnub pubnub = PubnubUnityUtils . NewUnityPubnub ( pnConfiguration ) ;
42
27
43
- //Parse operation response
44
- PNAccessManagerTokenResult grantTokenResult = grantTokenResponse . Result ;
45
- PNStatus grantTokenStatus = grantTokenResponse . Status ;
28
+ // If you're using Unity Editor setup you can get the Pubnub instance from PNManagerBehaviour
29
+ // For more details, see https://www.pubnub.com/docs/sdks/unity#configure-pubnub
30
+ /*
31
+ [SerializeField] private PNManagerBehaviour pubnubManager;
32
+ Pubnub pubnub = pubnubManager.pubnub;
33
+ */
46
34
47
- if ( ! grantTokenStatus . Error && grantTokenResult != null )
48
- {
49
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
50
- }
51
- else
52
- {
53
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
54
- }
55
- }
56
- catch ( Exception ex )
57
- {
58
- Console . WriteLine ( $ "Request cannot be executed due to error: { ex . Message } ") ;
59
- }
60
35
// snippet.end
61
36
}
62
37
@@ -73,9 +48,9 @@ static async Task GrantTokenComplex()
73
48
{ "channel-b" , new PNTokenAuthValues ( ) { Read = true , Write = true } } ,
74
49
{ "channel-c" , new PNTokenAuthValues ( ) { Read = true , Write = true } } ,
75
50
{ "channel-d" , new PNTokenAuthValues ( ) { Read = true , Write = true } } } ,
76
- ChannelGroups = new Dictionary < string , PNTokenAuthValues > ( ) {
51
+ ChannelGroups = new Dictionary < string , PNTokenAuthValues > ( ) {
77
52
{ "channel-group-b" , new PNTokenAuthValues ( ) { Read = true } } } ,
78
- Uuids = new Dictionary < string , PNTokenAuthValues > ( ) {
53
+ Uuids = new Dictionary < string , PNTokenAuthValues > ( ) {
79
54
{ "uuid-c" , new PNTokenAuthValues ( ) { Get = true } } ,
80
55
{ "uuid-d" , new PNTokenAuthValues ( ) { Get = true , Update = true } } }
81
56
} )
@@ -84,15 +59,15 @@ static async Task GrantTokenComplex()
84
59
PNStatus grantTokenStatus = grantTokenResponse . Status ;
85
60
if ( ! grantTokenStatus . Error && grantTokenResult != null )
86
61
{
87
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
62
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
88
63
}
89
64
else
90
65
{
91
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
66
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
92
67
}
93
68
// snippet.end
94
69
}
95
-
70
+
96
71
static async Task GrantTokenWithRegex ( )
97
72
{
98
73
// snippet.grant_token_regex
@@ -109,15 +84,15 @@ static async Task GrantTokenWithRegex()
109
84
PNStatus grantTokenStatus = grantTokenResponse . Status ;
110
85
if ( ! grantTokenStatus . Error && grantTokenResult != null )
111
86
{
112
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
87
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
113
88
}
114
89
else
115
90
{
116
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
91
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
117
92
}
118
93
// snippet.end
119
94
}
120
-
95
+
121
96
static async Task GrantTokenComplexWithRegex ( )
122
97
{
123
98
// snippet.grant_token_complex_with_regex
@@ -147,41 +122,41 @@ static async Task GrantTokenComplexWithRegex()
147
122
PNStatus grantTokenStatus = grantTokenResponse . Status ;
148
123
if ( ! grantTokenStatus . Error && grantTokenResult != null )
149
124
{
150
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
125
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
151
126
}
152
127
else
153
128
{
154
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
129
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
155
130
}
156
131
// snippet.end
157
132
}
158
-
133
+
159
134
static async Task GrantTokenOldBasicUsage ( )
160
135
{
161
136
// snippet.basic_usage_old
162
137
PNResult < PNAccessManagerTokenResult > grantTokenResponse = await pubnub . GrantToken ( )
163
138
. TTL ( 15 )
164
139
. AuthorizedUserId ( "my-authorized-userId" )
165
- . Resources ( new PNTokenResources ( )
140
+ . Resources ( new PNTokenResources ( )
166
141
{
167
142
Spaces = new Dictionary < string , PNTokenAuthValues > ( ) {
168
143
{ "my-space" , new PNTokenAuthValues ( ) { Read = true } } } // False to disallow
169
- } )
144
+ } )
170
145
. ExecuteAsync ( ) ;
171
146
PNAccessManagerTokenResult grantTokenResult = grantTokenResponse . Result ;
172
147
PNStatus grantTokenStatus = grantTokenResponse . Status ;
173
148
//PNAccessManagerTokenResult is a parsed and abstracted response from the server
174
149
if ( ! grantTokenStatus . Error && grantTokenResult != null )
175
150
{
176
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
151
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
177
152
}
178
153
else
179
154
{
180
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
155
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
181
156
}
182
157
// snippet.end
183
158
}
184
-
159
+
185
160
static async Task GrantTokenOldComplex ( )
186
161
{
187
162
// snippet.grant_token_complex_old
@@ -195,7 +170,7 @@ static async Task GrantTokenOldComplex()
195
170
{ "space-b" , new PNTokenAuthValues ( ) { Read = true , Write = true } } ,
196
171
{ "space-c" , new PNTokenAuthValues ( ) { Read = true , Write = true } } ,
197
172
{ "space-d" , new PNTokenAuthValues ( ) { Read = true , Write = true } } } ,
198
- Users = new Dictionary < string , PNTokenAuthValues > ( ) {
173
+ Users = new Dictionary < string , PNTokenAuthValues > ( ) {
199
174
{ "user-c" , new PNTokenAuthValues ( ) { Get = true } } ,
200
175
{ "user-d" , new PNTokenAuthValues ( ) { Get = true , Update = true } } }
201
176
} )
@@ -204,15 +179,15 @@ static async Task GrantTokenOldComplex()
204
179
PNStatus grantTokenStatus = grantTokenResponse . Status ;
205
180
if ( ! grantTokenStatus . Error && grantTokenResult != null )
206
181
{
207
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
182
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
208
183
}
209
184
else
210
185
{
211
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
186
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
212
187
}
213
188
// snippet.end
214
189
}
215
-
190
+
216
191
static async Task GrantTokenOldWithRegex ( )
217
192
{
218
193
// snippet.grant_token_old_regex
@@ -229,15 +204,15 @@ static async Task GrantTokenOldWithRegex()
229
204
PNStatus grantTokenStatus = grantTokenResponse . Status ;
230
205
if ( ! grantTokenStatus . Error && grantTokenResult != null )
231
206
{
232
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
207
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
233
208
}
234
209
else
235
210
{
236
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
211
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
237
212
}
238
213
// snippet.end
239
214
}
240
-
215
+
241
216
static async Task GrantTokenOldComplexWithRegex ( )
242
217
{
243
218
// snippet.grant_token_complex_old_with_regex
@@ -265,15 +240,15 @@ static async Task GrantTokenOldComplexWithRegex()
265
240
PNStatus grantTokenStatus = grantTokenResponse . Status ;
266
241
if ( ! grantTokenStatus . Error && grantTokenResult != null )
267
242
{
268
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
243
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenResult ) ) ;
269
244
}
270
245
else
271
246
{
272
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
247
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( grantTokenStatus ) ) ;
273
248
}
274
249
// snippet.end
275
250
}
276
-
251
+
277
252
static async Task RevokeTokenBasicUsage ( )
278
253
{
279
254
// snippet.revoke_token
@@ -285,11 +260,11 @@ static async Task RevokeTokenBasicUsage()
285
260
PNStatus revokeTokenStatus = revokeTokenResponse . Status ;
286
261
if ( ! revokeTokenStatus . Error && revokeTokenResult != null )
287
262
{
288
- Console . WriteLine ( "Revoke token success" ) ;
263
+ Debug . Log ( "Revoke token success" ) ;
289
264
}
290
265
else
291
266
{
292
- Console . WriteLine ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( revokeTokenStatus ) ) ;
267
+ Debug . Log ( pubnub . JsonPluggableLibrary . SerializeToJsonString ( revokeTokenStatus ) ) ;
293
268
}
294
269
// snippet.end
295
270
}
0 commit comments