-
Notifications
You must be signed in to change notification settings - Fork 3
/
Notifications.cs
96 lines (82 loc) · 3.31 KB
/
Notifications.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
using System;
using System.Collections.Generic;
using System.Net;
using CometD.NetCore.Bayeux;
using CometD.NetCore.Bayeux.Client;
using CometD.NetCore.Client;
using CometD.NetCore.Client.Transport;
using Genesys.Internal.Statistics.Api;
using Genesys.Internal.Statistics.Client;
namespace consolestatisticsappcsharp
{
public class Notifications : IMessageListener
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public delegate void CometDEventHandler(IClientSessionChannel channel, IMessage message, BayeuxClient client);
private BayeuxClient bayeuxClient;
private Dictionary<string, CometDEventHandler> subscriptions;
public Notifications()
{
subscriptions = new Dictionary<string, CometDEventHandler>();
}
public void Initialize(StatisticsApi api)
{
WebHeaderCollection headers = new WebHeaderCollection();
foreach (string key in api.Configuration.DefaultHeader.Keys)
{
switch (key)
{
case "x-api-key":
case "Authorization":
headers.Add(key, api.Configuration.DefaultHeader[key]);
break;
}
}
CookieCollection cookieCollection = CookieManager.Cookies.GetCookies(new Uri(api.GetBasePath()));
/**
* GWS currently only supports LongPolling as a method to receive events.
* So tell the CometD library to negotiate a handshake with GWS and setup a LongPolling session.
*/
LongPollingTransport transport = new LongPollingTransport(null);
transport.CookieCollection = cookieCollection;
transport.HeaderCollection = headers;
bayeuxClient = new BayeuxClient(api.GetBasePath() + "/notifications", new List<CometD.NetCore.Client.Transport.ClientTransport>() { transport });
bayeuxClient.Handshake();
bayeuxClient.WaitFor(30000, new List<BayeuxClient.State>() { BayeuxClient.State.Connected });
if (bayeuxClient.Connected)
{
foreach (Cookie cookie in cookieCollection)
{
CookieManager.AddCookie(cookie);
}
foreach (string channelName in subscriptions.Keys)
{
IClientSessionChannel channel = bayeuxClient.GetChannel(channelName);
channel.Subscribe(this);
}
}
}
public void subscribe(String channelName, CometDEventHandler eventHandler)
{
subscriptions.Add(channelName, eventHandler);
}
public void Disconnect()
{
if (bayeuxClient != null && bayeuxClient.Connected)
{
bayeuxClient.Disconnect();
}
}
public void OnMessage(IClientSessionChannel channel, IMessage message)
{
try
{
subscriptions[message.Channel](channel, message, null);
}
catch (Exception exc)
{
log.Error("Execption handling OnMessage for " + message.Channel, exc);
}
}
}
}