简体   繁体   English

使用 C# 监听文本/事件流

[英]Listening to text/event-stream using C#

I am writing a C# class to wrap around a simple web service.我正在编写一个 C# 类来包装一个简单的 Web 服务。 The RESTful stuff was easy to wrap up, but now I need to raise an event when something on the server changes. RESTful 的东西很容易总结,但现在我需要在服务器上的某些内容发生变化时引发一个事件。

I've set the server up to make an event stream available, but I don't know how to pick up the stream in C#.我已将服务器设置为使事件流可用,但我不知道如何在 C# 中获取该流。

Currently I'm dong something like this:目前我正在做这样的事情:

public class ServiceWrapper
{
    private readonly wc = new WebClient();

    public ServiceWrapper()
    {
        wc.OpenReadAsync(new Uri(UriOfEvent));
        wc.OpenReadCompleted += ServerEventOccurs;
    }

    private void ServerEventOccurs(object sender, OpenReadCompletedEventArgs args)
    {
        using (var sr = new StreamReader(args.Result))
        {
            var message = ParseStream(sr);
            RaiseServerEventOccurred(message);
        }

        wc.OpenReadAsync(new Uri(UriOfEvent));
    } 

    //usual code for declaring and raising ServerEventOccurred event
}

In my test the event gets picked up once, but not twice.在我的测试中,事件被拾起一次,但不是两次。 The event on the server is essentially a switch - something happens and it goes on, something else happens and it goes off.服务器上的事件本质上是一个开关 - 发生某些事情并继续,其他事情发生并关闭。 I know the switching works, because I've hooked the event stream up to a normal web page to test it.我知道切换有效,因为我已将事件流连接到普通网页以对其进行测试。

How should I be dealing with event streams in C#?我应该如何处理 C# 中的事件流?

Edit 1: I've updated the code to fix the bug TimVK points out, but the event stream still isn't being picked up the second time it should be.编辑 1:我已经更新了代码以修复 TimVK 指出的错误,但事件流仍然没有被第二次拾取。

Doesn't it work when you put your wc as a property in the class instead of creating always a new one in your methods?当您将wc作为类中的一个属性而不是在您的方法中始终创建一个新属性时,它不会起作用吗?

public class ServiceWrapper 
{ 
    WebClient wc {get;set;}
    public ServiceWrapper() 
    { 
        wc = new WebClient(); 
        wc.OpenReadAsync(new Uri(UriOfEvent)); 
        wc.OpenReadCompleted += ServerEventOccurs; 
    } 
 
    private void ServerEventOccurs(object sender, OpenReadCompletedEventArgs args) 
    { 
        using (var sr = new StreamReader(args.Result)) 
        { 
            var message = ParseStream(sr); 
            RaiseServerEventOccurred(message); 
        } 
 
        wc = new WebClient(); 
        wc.OpenReadAsync(new Uri(UriOfEvent)); 
    }  
 
    //usual code for declaring and raising ServerEventOccurred event 
} 

Then I suppose the event should be raised everytime.那么我想每次都应该引发这个事件。

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        WebClient myWebRequest = null;
        String Path = "http://192.168.196.106/home/events/";
        Uri uri = null;

        public Form1()
        {
            InitializeComponent();

            uri = new Uri(Path);

            this.myWebRequest = new WebClient();
            this.myWebRequest.Headers.Set("Host", "192.168.196.106");

            this.myWebRequest.OpenReadCompleted += MyWebRequest_OpenReadCompleted;
        }

        private async void MyWebRequest_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            using (var sr = new StreamReader(e.Result))
            {
                try
                {
                    while (true)
                    {
                        var message = await sr.ReadLineAsync();
                        Debug.WriteLine("message = {0}", (object)message);
                    }
                }
                catch (WebException ex)
                {
                    Debug.WriteLine("WebException: {0}", (object)ex.Message);
                }
                catch (IOException ex)
                {
                    Debug.WriteLine("IOException: {0}", (object)ex.Message);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception: {0}", (object)ex.Message);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myWebRequest.OpenReadAsync(uri);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            myWebRequest.CancelAsync();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM