-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStreamingEvents.cs
193 lines (164 loc) · 5.53 KB
/
StreamingEvents.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
/*
* These classes are examples for using Oyatels streaming events API through CometD.
* */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cometd.Client;
using Cometd.Client.Transport;
using Cometd.Bayeux;
using Cometd.Bayeux.Client;
using Cometd.Common;
namespace Oyatel.Connect.Tutorial
{
// Delegate for callevents:
public delegate void CallEvent(IDictionary<String, Object> data);
// Delegate for events taking bool parameter:
public delegate void BoolEvent(bool success);
/*
* Subscribe and listen to connected/disconnected-events.
* */
public class ConnectionListener : IMessageListener
{
public bool connected = false;
public ConnectionListener()
: base()
{
}
public void onMessage(IClientSessionChannel channel, IMessage message)
{
if (message.Successful)
{
if (!connected)
{
connected = true;
}
}
else
{
if (connected)
{
connected = false;
}
}
}
}
/*
* Subscribe and listen to logincompleted-events.
* */
public class InitializerListener : IMessageListener
{
private event BoolEvent onLoginCompleted;
public String userId = "";
public String username = "";
public InitializerListener(BoolEvent onLoginCompleted)
: base()
{
this.onLoginCompleted += onLoginCompleted;
}
public void onMessage(IClientSessionChannel channel, IMessage message)
{
try
{
IDictionary<String, Object> ext = (IDictionary<String, Object>)message.Ext["authentication"];
userId = ext["userId"].ToString();
username = ext["username"].ToString();
}
catch (Exception)
{
}
onLoginCompleted(message.Successful);
}
}
/*
* Subscribe and listen to callevents.
* */
public class BatchCallEventListener : IMessageListener
{
public BayeuxClient client;
private event CallEvent onCallEvent;
public BatchCallEventListener(BayeuxClient client, CallEvent onCallEvent)
{
this.client = client;
this.onCallEvent = onCallEvent;
}
public void Run()
{
IClientSessionChannel callEventChannel = client.getChannel("/events/call");
callEventChannel.unsubscribe(this);
callEventChannel.subscribe(this);
}
public void onMessage(IClientSessionChannel channel, IMessage message)
{
try
{
IDictionary<String, Object> data = message.DataAsDictionary;
// Trigger callback:
onCallEvent(data);
}
catch (Exception)
{
}
}
}
/*
* Main class for setting up streaming events.
* */
public class StreamingEvents
{
protected BayeuxClient client;
protected InitializerListener initListener;
protected ConnectionListener connectionListener = null;
protected String url = "https://api.oyatel.com:443/cometd/cometd";
private CallEvent onCallEvent;
/*
* Set up, log-in and listen to streaming events from Oyatel.
* This example listens for call-events.
* */
public StreamingEvents(CallEvent onCallEvent)
{
this.onCallEvent = onCallEvent;
}
public void Connect(String access_token)
{
IList<ClientTransport> transports = new List<ClientTransport>();
transports.Add(new LongPollingTransport(null));
client = new BayeuxClient(url, transports);
// Subscribe and call 'Initialize' after successful login
initListener = new InitializerListener(Initialize);
client.getChannel(Channel_Fields.META_HANDSHAKE).addListener(initListener);
// Subscribe to connect/disconnect-events
connectionListener = new ConnectionListener();
client.getChannel(Channel_Fields.META_CONNECT).addListener(connectionListener);
// Handshaking with oauth2
IDictionary<String, Object> handshakeAuth = new Dictionary<String, Object>();
handshakeAuth.Add("authType", "oauth2");
handshakeAuth.Add("oauth_token", access_token);
IDictionary<String, Object> ext = new Dictionary<String, Object>();
ext.Add("ext", handshakeAuth);
client.handshake(ext);
client.handshake(handshakeAuth);
}
public void Disconnect()
{
client.disconnect();
}
public bool Connected
{
get
{
if (connectionListener == null) return false;
return connectionListener.connected;
}
}
private void Initialize(bool success)
{
if (success)
{
BatchCallEventListener batch = new BatchCallEventListener(client, onCallEvent);
client.batch(new BatchDelegate(batch.Run));
}
}
}
}