开发者

Stream text to client via handler ASP.NET

To get around twitters streaming API not having a crossdomain file to access it from client side( in this case Silverlight) I have made a Generic Handler file in a web project which basically downloads the stream from twitter and as it reads it, writes it to the client. Here is the handler code:

 context.Response.Buffer = false;
        context.Response.ContentType = "text/plain";

        WebRequest request = WebRequest.Create("http://stream.twitter.com/1/statuses/filter.json?locations=-180,-90,180,90");
        request.Credentials = new NetworkCredential("username", "password");
        StreamReader responseStream = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.GetEncoding("utf-8"));

        while (!responseStream.EndOfStream)
        {
                string line = "(~!-/" + responseStream.ReadLine() + "~!-/)";
               context.Response.BinaryWrite((Encoding.UTF8.GetBytes(line)));}

And this does work, but the problem is that once the client disconnects the handler just carry's on downloading. So how do I tell if the client is still busy receiving the request and if not, end the while loop?

Also, my second problem is that on the client side doing a "ReadLine()" does nothing, presumably because it is counting the entire stream as one line so never gets the full response. To work around that I read it byte by byte and when it sees "(~!-/" around something it k开发者_StackOverflow社区now that is one line. VERY hacky, I know.

Thanks!


Found the answer!

while (context.Response.IsClientConnected)

:)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜