forked from MaartenStaa/mow-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebsync.cs
120 lines (106 loc) · 3.5 KB
/
Websync.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
using System;
using FM.WebSync;
using MowChat.Data;
using RestSharp;
using RestSharp.Deserializers;
namespace MowChat
{
public class Websync
{
/// <summary>
/// The websync server
/// </summary>
private const string WebsyncUrl = "http://igmchatlxx.isotx.com/websync.ashx";
/// <summary>
/// The WebSync connection.
/// </summary>
private Client _client;
/// <summary>
/// The instance of WebSync manager singleton.
/// </summary>
private static Websync _instance;
/// <summary>
/// The instance of WebSync manager singleton.
/// </summary>
public static Websync Instance
{
get { return _instance ?? (_instance = new Websync()); }
}
/// <summary>
/// Whether the WebSync manager singleton exists.
/// </summary>
public static bool HasInstance
{
get { return _instance != null; }
}
/// <summary>
/// Constructor.
/// </summary>
private Websync()
{
ConnectToWebsync();
}
/// <summary>
/// Connect to the WebSync server.
/// </summary>
private void ConnectToWebsync()
{
_client = new Client(WebsyncUrl);
_client.Connect(new ConnectArgs
{
OnSuccess = data => Logger.Print("Connected to WebSync."),
});
}
/// <summary>
/// Disconnect from WebSync.
/// </summary>
public void DisconnectFromWebsync()
{
if (!_client.IsConnected) return;
_client.Disconnect();
_instance = null;
}
/// <summary>
/// Subscribe to a WebSync channel.
/// </summary>
/// <param name="channelName">The name of the channel to subscribe to.</param>
/// <param name="callback">The function to call when a message is received.</param>
public void Subscribe(string channelName, Action<WebsyncMessage> callback)
{
_client.Subscribe(
new SubscribeArgs(channelName)
{
OnReceive = data =>
{
Logger.Print("Received from WebSync, " + data.DataJson);
var wrapped = FromJson<WrappedWebsyncMessage>(data.DataJson);
WebsyncMessage message;
switch (wrapped.m_version)
{
// Case 31668.
case 1:
message = FromJson<WebsyncMessage>(wrapped.m_dataJsonS);
break;
// Initial format did not specify version.
default:
message = FromJson<WebsyncMessage>(string.Join("", wrapped.m_dataJson));
break;
}
callback(message);
}
});
}
/// <summary>
/// Deserialize a JSON string to an object of type T.
/// </summary>
/// <typeparam name="T">The expected object type.</typeparam>
/// <param name="json">The JSON string.</param>
/// <returns>An instance of T.</returns>
private static T FromJson<T>(string json)
{
// Abuse RestSharp to deserialize JSON for us.
var response = new RestResponse<T> { Content = json };
return new JsonDeserializer().Deserialize<T>(response);
}
}
}