Last active
January 16, 2017 15:12
-
-
Save yuka1984/d83999dc0f48921479b59baf88740da5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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