Push content to client after page is loaded - c#

I wonder if the following is possible:
I have a dll that I have referenced in my web site. This dll makes a remote socket connection. The socket connection is waiting in the background and the dll reports back data through events after the socket has received some data.
The connection is opened during load of page.
Now, I would, for example, like to update a label on the page when new data has arrived.
I am not sure how this would work. I assume that I could set some kind of timer on page that updates a control but it does not seem "optimal" as I already call code behind through my events. "Optimally" the UpdatePanel or whatever updates the interface would wait for events and "update" when events has occurred and not based on time.
My question is - is this possible?

You can use techniques called "long pulling" or "web sockets".
There are libraries, like SignalR that can help you.
They generally use web sockets when client's browser supports and long pulling when not.
Using these libraries you can "push" commands/data from server to client's browser, just as you want.

Server cannot push the client, this is the rule how web works.
There are two possible ways to complete your task.
Put timer on the page and make requests on server too see, if something changed
Open long pulling connection between client and server, like facebook does. That listens
to the events and gets data from the server. It can be, for example xmpp or any other.

You should think about turning this around; rather than initiate the call from the code behind asynchronously and then update the client, you should deliver your original page to the client.
Once the page has been loaded client side, you can make an AJAX call to the server to retrieve the data you want - and display a little "I'm loading" symbol while this happens.
Page Methods are ideal for this.
The classic ASP.Net web page is supposed to last for the duration of the users request. When a postback happens, it effectively rebuilds the state of the call from the user's viewstate, session and any other state you've maintained. It never cares if the user goes away, for example (although it might feel a bit lonely).
Having it hang around for longer is problematic in a number of ways, however, you can implement client callbacks. However, although the server initiates this, the client manages the lifecycle, so it's analogous to page methods.

Related

Asp.net MVC - Push a deferred client message from server

I have an intranet/extranet app which calls the server to do some VERY LONG calculations. The client should be able to keep surfing the site and doing his stuff, until a message pops up saying the calculation has ended and asking him if he wants to go back to the page to see the results.
If so, the client must be redirected to that page, the data must be retrieved the from the server and displayed. If the user cancels the action, the data set must be cleared.
How do I send a message to the client from C# and how do I catch it? Is it possible?
Thanks
Reactive Extensions (Rx) for .NET deals with exactly this kind of problem. It allows for "push" notifications of remote events.
Here is a blog post that introduces Rx over ASP.NET:
http://weblogs.asp.net/podwysocki/archive/2010/02/16/introduction-to-the-reactive-extensions-to-javascript.aspx
Here's an entire RxJS library:
http://reactive-extensions.github.io/RxJS/
Yes, sending messages from server to client via http possible with using web-sockets or longpolling technologies.

What is best & Secure timer for online test application in asp.net C# Ajax timer or Jquery Timer?

I am creating a new application in asp.net 4.0 which contains an online test module, and the application is supposed to handle 10-20k students at a time. Now my confusion is what should i use here, which should be:
Secure , I don't want kids to turn off javascript in browser and get unlimited time for test.
Which should not overhead the network traffic.
Which have a pause/start function with try limits.
Should fire the needed function on server side after time is complete (e.g. redirect to another page via c# function)
Any help and example code is appreciated.
Regards
Alok Sharma
FYI: I am not experienced with jquery.
If the security of timer is critical you should not fully rely on javascript code. I would recommend to keep the time when test is started for a specific user on both sides, server and client. Then implement javascript timer which periodically synchronizes with web server. When user completes the test and sends results to the server you should check if the timeout is elapsed on server side.
In additional I would add a logic in order to ban the possibility to open the page and start test without javascript enabled.

Using C# to efficiently pull data from a webpage with changing sourcecode?

I have already put together code using the System.net.Webclient class to pull source code from a webpage, which I then use a string search on, to get specific information. This in itself works fine, but my issue is that the source code changes every few seconds, and I would like the data I have received to change accordingly. I understand that I could simply set up a loop to have this process repeat, but unfortunately my current code take a full 2.7 seconds to complete, and I would like to avoid this large lag time. In addition, I want to avoid spamming the webpage with requests if possible. I was thinking about a streamread that stays open, so that multiple requests wouldn't have to be sent, but I wasn't entirely sure how to go about this...
So to sum it up, is there a way that I can pull updating information from a website using the System.Net namespace in a manner that is both fast, and avoids spamming the website with requests?
I am afraid that HTTP protocol is not adapted to your real-time data refresh requirement. Other than polling with HTTP requests at regular intervals you cannot know whether the data changed on the server and get this fresh data.
For example the WebSocket technology is more adapted to those scenarios. Of course the data provider must implement it so that clients could subscribe to this live feed.
There's also another way to implement this feature over the HTTP protocol. It uses an iframe to implement long polling. Here's an example. The idea is that the server uses chunked transfer encoding and sends continuous streams of data to the socket. The client subscribes to this stream and is able to be notified of changes occurring on the server. Once again, it's a technology that must be implemented by the server side so that you, as a client, could take advantage of it.
If all that the server provides is data via HTML page you are doomed to do screen scraping by hammering this server with HTTP requests until your IP address gets black listed and denied access.

Custom C# Webserver cannot accept simultaneous ajax calls

I have written a custom web server for a web application I am developing, however, I am stumped on a weird problem I seem to be having with .NET sockets.
For webpages the server works completely fine but when making an ajax call to the server to perform a function that has a lengthy processing time, the server is unable to accept another ajax call from the same session.
The process is as such:
A button press on the website causes an ajax call to http://localhost:900/function:index (My server recognizes this) which performs an lengthy operation returning "success" when it finishes.
If I push the button again before the initial call returns, the browser makes the same call to the server but this time, Socket.Accept() does not return a new socket and there is no indication that there is a connection waiting. (Firebug does show a connection attempt though)
However, if I refresh the page or click a link to load a new page, it works as expected and the button works as expected (returning a fail as the operation is still continuing)
To sum it up: Socket.Accept() does not return a new socket if an existing, identical ajax call is still processing.
I am stumped... Any ideas as to why this may be?
Well, it appears that the answer lay in the browser. By adding an arbitrary time argument onto the end of the url so that every request was unique, it worked.
For whatever reason, the browser would not send two simultaneous ajax calls at once.

Telling jQuery to reload page when there is a server side event

I have a page with a div and the elements within the div get updated from the server side periodically. For example, I have a progress bar in it. Currently, I'm doing jQuery.load using setInterval to periodically refresh the div on the page (every 3 seconds). I was wondering if I could achieve this using an event-based method rather than a timer-based. For instance, I could have a server event fire everytime the progress changes, and I could then increase the width of the progress bar on the page (and change other elements as well). Question is how can I tell jQuery to do a load when this happens? Is there a jquery onchange event or somethign that watches for page changes?
Polling could be done, but there are a better way: Comet servers. Basically the HTTP server allows long running requests which means that it do not send the response unless something have happened or the timeout is about to kick in.
Read about comet here: http://en.wikipedia.org/wiki/Comet_(programming))
A comet plugin for jquery: http://plugins.jquery.com/project/Comet
A more technical article: http://ajaxpatterns.org/HTTP_Streaming
This is simply not possible because the underlying communication protocol (HTTP) between server & client is request-response based. So it is not possible for server to notify client w/o client making request for it. In short, polling is the way to go.

Categories