/
Pubnub.cs
396 lines (349 loc) · 10.3 KB
/
Pubnub.cs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Web.Script.Serialization;
/**
* PubNub 3.0 Real-time Push Cloud API
*
* @author Stephen Blum
* @package pubnub
*/
public class PubnubTEST {
static public void Main() {
// -----------------
// Init Pubnub Class
// -----------------
Pubnub pubnub = new Pubnub(
"demo", // PUBLISH_KEY
"demo", // SUBSCRIBE_KEY
"", // SECRET_KEY
false // SSL_ON?
);
string channel = "test-channel-顶";
// ---------------
// Publish Message
// ---------------
List<object> info = pubnub.Publish( channel, "Hello World" );
// ----------------
// Publish Response
// ----------------
Console.WriteLine(
"Publish Success: " + info[0].ToString() +
"\nPublish Info: " + info[1]
);
// -------
// History
// -------
List<object> history = pubnub.History( channel, 1 );
foreach (object history_message in history) {
Console.Write("History Message: ");
Console.WriteLine(history_message);
}
// ----------------------
// Get PubNub Server Time
// ----------------------
object timestamp = pubnub.Time();
Console.WriteLine("Server Time: " + timestamp.ToString());
// ---------
// Subscribe
// ---------
pubnub.Subscribe(
channel,
delegate (object message) {
Console.WriteLine("Received Message -> '" + message + "'");
return true;
}
);
}
}
/**
* PubNub 3.0 Real-time Push Cloud API
*
* @author Stephen Blum
* @package pubnub
*/
public class Pubnub {
private string ORIGIN = "pubsub.pubnub.com";
private int LIMIT = 1800;
private string PUBLISH_KEY = "";
private string SUBSCRIBE_KEY = "";
private string SECRET_KEY = "";
private bool SSL = false;
public delegate bool Procedure(object message);
/**
* PubNub 3.0
*
* Prepare PubNub Class State.
*
* @param string Publish Key.
* @param string Subscribe Key.
* @param string Secret Key.
* @param bool SSL Enabled.
*/
public Pubnub(
string publish_key,
string subscribe_key,
string secret_key,
bool ssl_on
) {
this.init( publish_key, subscribe_key, secret_key, ssl_on );
}
/**
* PubNub 2.0 Compatibility
*
* Prepare PubNub Class State.
*
* @param string Publish Key.
* @param string Subscribe Key.
*/
public Pubnub(
string publish_key,
string subscribe_key
) {
this.init( publish_key, subscribe_key, "", false );
}
/**
* PubNub 3.0 without SSL
*
* Prepare PubNub Class State.
*
* @param string Publish Key.
* @param string Subscribe Key.
* @param string Secret Key.
*/
public Pubnub(
string publish_key,
string subscribe_key,
string secret_key
) {
this.init( publish_key, subscribe_key, secret_key, false );
}
/**
* Init
*
* Prepare PubNub Class State.
*
* @param string Publish Key.
* @param string Subscribe Key.
* @param string Secret Key.
* @param bool SSL Enabled.
*/
public void init(
string publish_key,
string subscribe_key,
string secret_key,
bool ssl_on
) {
this.PUBLISH_KEY = publish_key;
this.SUBSCRIBE_KEY = subscribe_key;
this.SECRET_KEY = secret_key;
this.SSL = ssl_on;
// SSL On?
if (this.SSL) {
this.ORIGIN = "https://" + this.ORIGIN;
}
else {
this.ORIGIN = "http://" + this.ORIGIN;
}
}
/**
* History
*
* Load history from a channel.
*
* @param String channel name.
* @param int limit history count response.
* @return ListArray of history.
*/
public List<object> History( string channel, int limit ) {
List<string> url = new List<string>();
url.Add("history");
url.Add(this.SUBSCRIBE_KEY);
url.Add(channel);
url.Add("0");
url.Add(limit.ToString());
return _request(url);
}
/**
* Publish
*
* Send a message to a channel.
*
* @param String channel name.
* @param List<object> info.
* @return bool false on fail.
*/
public List<object> Publish( string channel, object message ) {
JavaScriptSerializer serializer = new JavaScriptSerializer();
// Generate String to Sign
string signature = "0";
if (this.SECRET_KEY.Length > 0) {
StringBuilder string_to_sign = new StringBuilder();
string_to_sign
.Append(this.PUBLISH_KEY)
.Append('/')
.Append(this.SUBSCRIBE_KEY)
.Append('/')
.Append(this.SECRET_KEY)
.Append('/')
.Append(channel)
.Append('/')
.Append(serializer.Serialize(message));
// Sign Message
signature = md5(string_to_sign.ToString());
}
// Build URL
List<string> url = new List<string>();
url.Add("publish");
url.Add(this.PUBLISH_KEY);
url.Add(this.SUBSCRIBE_KEY);
url.Add(signature);
url.Add(channel);
url.Add("0");
url.Add(serializer.Serialize(message));
// Return JSONArray
return _request(url);
}
/**
* Subscribe
*
* This function is BLOCKING.
* Listen for a message on a channel.
*
* @param string channel name.
* @param Procedure function callback.
*/
public void Subscribe( string channel, Procedure callback ) {
this._subscribe( channel, callback, 0 );
}
/**
* Subscribe - Private Interface
*
* @param string channel name.
* @param Procedure function callback.
* @param string timetoken.
*/
private void _subscribe(
string channel,
Procedure callback,
object timetoken
) {
// Begin Recusive Subscribe
try {
// Build URL
List<string> url = new List<string>();
url.Add("subscribe");
url.Add(this.SUBSCRIBE_KEY);
url.Add(channel);
url.Add("0");
url.Add(timetoken.ToString());
// Wait for Message
List<object> response = _request(url);
// Update TimeToken
if (response[1].ToString().Length > 0)
timetoken = (object)response[1];
// Run user Callback and Reconnect if user permits.
foreach (object message in (object[])response[0]) {
if (!callback(message)) return;
}
// Keep listening if Okay.
this._subscribe( channel, callback, timetoken );
}
catch {
System.Threading.Thread.Sleep(1000);
this._subscribe( channel, callback, timetoken );
}
}
/**
* Time
*
* Timestamp from PubNub Cloud.
*
* @return object timestamp.
*/
public object Time() {
List<string> url = new List<string>();
url.Add("time");
url.Add("0");
List<object> response = _request(url);
return response[0];
}
/**
* Request URL
*
* @param List<string> request of url directories.
* @return List<object> from JSON response.
*/
private List<object> _request(List<string> url_components) {
string temp = null;
int count = 0;
byte[] buf = new byte[8192];
StringBuilder url = new StringBuilder();
StringBuilder sb = new StringBuilder();
JavaScriptSerializer serializer = new JavaScriptSerializer();
// Add Origin To The Request
url.Append(this.ORIGIN);
// Generate URL with UTF-8 Encoding
foreach ( string url_bit in url_components) {
url.Append("/");
url.Append(_encodeURIcomponent(url_bit));
}
// Fail if string too long
if (url.Length > this.LIMIT) {
List<object> too_long = new List<object>();
too_long.Add(0);
too_long.Add("Message Too Long.");
return too_long;
}
// Create Request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url.ToString());
// Set Timeout
request.Timeout = 200000;
request.ReadWriteTimeout = 200000;
// Receive Response
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
// Read
do {
count = resStream.Read( buf, 0, buf.Length );
if (count != 0) {
temp = Encoding.UTF8.GetString( buf, 0, count );
sb.Append(temp);
}
} while (count > 0);
// Parse Response
string message = sb.ToString();
return serializer.Deserialize<List<object>>(message);
}
private string _encodeURIcomponent(string s) {
StringBuilder o = new StringBuilder();
foreach (char ch in s.ToCharArray()) {
if (isUnsafe(ch)) {
o.Append('%');
o.Append(toHex(ch / 16));
o.Append(toHex(ch % 16));
}
else o.Append(ch);
}
return o.ToString();
}
private char toHex(int ch) {
return (char)(ch < 10 ? '0' + ch : 'A' + ch - 10);
}
private bool isUnsafe(char ch) {
return " ~`!@#$%^&*()+=[]\\{}|;':\",./<>?".IndexOf(ch) >= 0;
}
private static string md5(string text) {
MD5 md5 = new MD5CryptoServiceProvider();
byte[] data = Encoding.Default.GetBytes(text);
byte[] hash = md5.ComputeHash(data);
string hexaHash = "";
foreach (byte b in hash) hexaHash += String.Format("{0:x2}", b);
return hexaHash;
}
}