Skip to content

Instantly share code, notes, and snippets.

@yuka1984
Last active January 16, 2017 15:12
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 yuka1984/d83999dc0f48921479b59baf88740da5 to your computer and use it in GitHub Desktop.
Save yuka1984/d83999dc0f48921479b59baf88740da5 to your computer and use it in GitHub Desktop.
using System;
using System.Text;
using Microsoft.Azure.EventHubs;
using Newtonsoft.Json;
namespace WebSocketChatSample
{
public class SendChatMessageToEventHubsObserver : IObserver<ChatMessage>
{
private EventHubClient client;
public string EhConnectionString { get; set; }
public string EhEntityPath { get; set; }
public void OnCompleted()
{
}
public void OnError(Exception error)
{
}
public async void OnNext(ChatMessage value)
{
try
{
if (client == null)
{
var connectionStringBuilder = new EventHubsConnectionStringBuilder(EhConnectionString)
{
EntityPath = EhEntityPath
};
client = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString());
}
await client.SendAsync(new EventData(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(value))));
}
catch (Exception e)
{
Console.WriteLine(e);
client = null;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment