2

I want to implement HTML5 Server Sent Event feature with ASP.NET Web Forms with Web Service.

My client code is

$(document).ready(function () {
            initialize();
        });
        function initialize() {
            if (window.EventSource == undefined) {
                document.getElementById('OutputDiv').innerHTML = "Your browser doesn't support Server Side Events.";
                return;
            }
            var source = new EventSource("WebSercvice.asmx");
            source.onopen = function (event) {
                document.getElementById('OutputDiv').innerHTML += 'Connection Opened.<br>';
            };
            source.onerror = function (event) {
                if (event.eventPhase == EventSource.CLOSED) {
                    document.getElementById('OutputDiv').innerHTML += 'Connection Closed.<br>';
                }
            };
            source.onmessage = function (event) {
                document.getElementById('OutputDiv').innerHTML += event.data + '<br>';
            };

and Server side code is

[WebMethod]
    public void HelloWorld() {
        DateTime startDate = DateTime.Now;
        HttpContext.Current.Response.ContentType = "text/event-stream";
        while (startDate.AddMinutes(1) > DateTime.Now)
        {
            HttpContext.Current.Response.Write(string.Format("data: {0}\n\n", startDate));
            HttpContext.Current.Response.Flush();
            System.Threading.Thread.Sleep(1000);
        }
    }

But it is not able to call the service method.

mason
  • 31,774
  • 10
  • 77
  • 121
user2720652
  • 33
  • 2
  • 9
  • This question is too broad for SO. What have you tried yourself? Also, see [How to implement real time data for a web page](http://stackoverflow.com/questions/25829343/how-to-implement-real-time-data-for-a-web-page). – mason Mar 10 '15 at 16:47
  • This is not correct answer.. Just want to refresh updated data using Server Sent Event.. – user2720652 Mar 11 '15 at 13:57
  • Your question is too broad, that's why it was closed. If you want to refresh data using Server Sent Events, then just do it. If you run into a specific problem with your implementation, then that would be an appropriate question for SO because it's narrow enough in scope to be answerable. But rolling your own library for SSE is probably not a good idea, which is why I linked you to my question/answers which shows several techniques for implementing real time data, including SignalR which is an abstraction over SSE/polling/Web Sockets that makes real time data in .NET easy. – mason Mar 11 '15 at 13:59
  • Thank you for quick response. I stuck by implementing SSE in ASP.net using Web Service asmx. I want to call Web Service Method using SSE and I tried all option but failed. below is code [WebMethod] public void HelloWorld() { DateTime startDate = DateTime.Now; HttpContext.Current.Response.ContentType = "text/event-stream"; while (startDate.AddMinutes(1) > DateTime.Now) {HttpContext.Current.Response.Write(string.Format("data: {0}\n\n", startDate)); HttpContext.Current.Response.Flush();System.Threading.Thread.Sleep(1000); } } – user2720652 Mar 11 '15 at 14:26
  • No, code does *not* belong in comments. Edit your question to have the code. Give the code and describe how it's not working as described. Once you have a decent question I'll nominate the question to be re-opened (let me know by commenting when you're done editing). – mason Mar 11 '15 at 14:27
  • Updated question. Please reply and also let me know how to make database call to get updated records.. because If i do simple database call it will try to open database connection and close every minute time.. please suggest – user2720652 Mar 11 '15 at 14:34
  • You didn't say how it's failing. Are you getting a client side error? – mason Mar 11 '15 at 14:37
  • I am getting below javascript error. in the url, I am also setting webmethod name. EventSource's response has a MIME type ("text/html") that is not "text/event-stream". Aborting the connection. – user2720652 Mar 11 '15 at 14:41
  • If you look at `var source = new EventSource("WebSercvice.asmx");` you spelled Service wrong, so that might be a problem. Also, you have to put the method name after the file path, ex: `var source = new EventSource("WebService.asmx/HelloWorld");` But anyways, you shouldn't be using ASMX anymore, that technology has been deprecated in favor of [Web API](http://www.asp.net/web-api). And real time web stuff should be done with [SignalR](http://signalr.net/). If you try to create your own SSE library, you'll get the implementation details wrong. – mason Mar 11 '15 at 14:44
  • I correct the name and path as suggested but still the same issue. I can't user SignalR as I am using visual studio 2008. I want to use SSE only with Web Service. Could you please help me to invoke the webmethod. – user2720652 Mar 11 '15 at 14:59
  • SignalR is a library, it has nothing to do with what version of VS you're using. You can use it with VS 2008. And anyways, [VS 2013 Web](https://www.visualstudio.com/) is free (so is VS 2013 Community if you're not a big company), and you're missing a number of advances if you're not using it. – mason Mar 11 '15 at 15:02
  • I can't use VS2013 here as need to take too many approvals to update framework and code. We will plan next year to migrate with VS13 but now i want to use SSE.. Can you please help me. – user2720652 Mar 11 '15 at 15:05
  • I have helped you. I suggested a well supported library that makes real time data possible and easy (and you unfathomably choose to ignore it), and I voted to re-open your question. As far as specifically helping you with SSE, I'm deeply familiar with the protocol so I won't be of much help. – mason Mar 11 '15 at 15:07

0 Answers0