Skip to content

Instantly share code, notes, and snippets.

@YuriyZaletskyy
Created November 23, 2020 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuriyZaletskyy/5dff485f3fcc787841b71e1e40cd6336 to your computer and use it in GitHub Desktop.
Save YuriyZaletskyy/5dff485f3fcc787841b71e1e40cd6336 to your computer and use it in GitHub Desktop.
public partial class Form1 : Form
{
private HubConnection connection;
public Form1()
{
InitializeComponent();
}
private void button_start_Click(object sender, EventArgs e)
{
var login = "admin";
var tenant = "Cmp1";
var password = "123";
// Set up a Basic authentication token
var basicAuthToken = Convert.ToBase64String(Encoding.UTF8.GetBytes(login + "@" + tenant + ":" + password));
//Connect to an Acumatica ERP instance
connection = new HubConnection("https://localhost/PushNotifications");
connection.Headers.Add("Authorization", "Basic " + basicAuthToken);
//Create a proxy to hub
//Use "PushNotificationsHub" as the address of the hub
var myHub = connection.CreateHubProxy("PushNotificationsHub");
connection.Start().ContinueWith(task =>
{
try
{
if (task.IsFaulted)
{
MessageBox.Show("There was an error during open of the connection: " +
task.Exception.GetBaseException());
}
else
{
//Instead of "TestNotification", specify the name
//that you specified on the Push Notifications form
myHub.Invoke<string>("Subscribe", "TestNotification").Wait();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message.ToString());
}
}).Wait();
myHub.On<NotificationResult>("ReceiveNotification", nr =>
{
// updated data
if (nr.Inserted.Length > 0 && nr.Deleted.Length > 0)
{
MessageBox.Show("Before update " + JsonConvert.DeserializeObject(nr.Deleted[0].ToString()));
MessageBox.Show("After update " + JsonConvert.DeserializeObject(nr.Inserted[0].ToString()));
}
else
{
// inserted data
if (nr.Inserted.Length > 0)
MessageBox.Show("Inserted " + JsonConvert.DeserializeObject(nr.Inserted[0].ToString()));
// deleted data
if (nr.Deleted.Length > 0)
MessageBox.Show("Deleted " + JsonConvert.DeserializeObject(nr.Deleted[0].ToString()));
}
});
button_start.Enabled = false;
button_stop.Enabled = true;
}
private void button_stop_Click(object sender, EventArgs e)
{
connection.Stop();
button_start.Enabled = true;
button_stop.Enabled = false;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
connection.Stop();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment