Asyncronously receiving data in an ASP.NET MVC application - c#

I'm looking for the best way to constantly check if data has been sent to our ASP.NET MVC application from a server. I would like to asynchronously check for data and update the view when data has been read.
I'm currently using a TcpClient to make a connection, and making the connection in the inherited custom base controller. Currently though I have to refresh the page for the updating of the view to take place.
Would there be a proper AJAX solution? or do I need to move the connection to the ActionMethod?

For complete solution use this free library : PokeIn

I would probably use jQuery to query some action at a specified interval and update the page accordingly.
I don't know what kind of data you're talking about, but let's look at a simple example. From you're question, it sounds to me like you're trying to do the following:
Suppose you are building Twitter and want to show the newest Tweets on the homepage. Well, you could have a script that just does a jQuery.GetJSON call to some URL (let's call it twitter.com/latest5tweets) that gets the latest 5 tweets. Then you would have a callback function that would take those tweets and paint them on the screen -- perhaps using some sort of nifty slide effect to remove the older ones from the screen and add the new ones.
Does that help?

Related

React to phonecall on Windows 10 Mobile

Is there any way of controlling an incoming phonecall on UWP? I'm looking for something like:
1. Get IncomingPhonecallEventHandler to attach
2. When triggered, be capable of:
phonecall.Accept();
phonecall.Decline();
phonecall.DeclineWithSMS();
All of this is a part of my android smartwatch project, so I need to be able to call and recieve calls as well.
EDIT 1
I know the PhoneCallManager class, but registering for CallStateChanged event returns object, which I don't know how to use.
I believe you are looking for the PhoneCallManager class and the PhoneCallManager.CallStateChanged event.
You can also checkout this example (in C#) to see how to interact with the apis
If you want to be able to block and filter calls, you can checkout this example, it has that capability.
If you want to send SMS, this example has the code needed
However, I don't believe there is an API to answer phone calls at this time

When is content updated?

Let's say I would like to change the text of an element, during some long function in a script:
// Inside test.aspx.cs
void SomeLongFunction_CalledOnClick()
{
this.idOfElement.Text = "something";
}
When, precisely, is the client sent information about this update? Can I force it to happen earlier?
If you are using web forms, the content of the browser is updated when the "Render" stage of the page life cycle runs. Generally the code in a web form's code behind runs before the render stage.
Here is a link that explains the page lifecycle
I'm afraid that if you are looking to update the UI to indicate progress, the web forms model isn't made to work that way. All the content is sent to the browser as a single unit during the render phase.
If you want your UI to show progress you could poll for updates using ajax calls, or use a technology like SignalR (although it's probably overkill for your use case).

WebBrowserDocumentCompletedEventHandler Don't wait for Ajax Async to complete

After reading Dianyang Wu article and this excellent post I managed to build a small .net app (almost a C&P from Wu source code) to automatize tests (let's call it protoTestApp). My final goal is to open a dozen small windows and in each simulate a different user interacting with a web app to stress it.
It works for some extend but after I logon on the web app (let's call it InternalTestSubject) it calls a external url (let's call it ExternalTestSubject) and injects it's content on a iFrame. This particular external url is another web app and it ill look up for the parent window to get some parameters. Opening ExternalTestSubject directly is not a option.
My problem is at my protoTestApp I want to also interact with that ExternalTestSubject (find a button by id, click it, etc) but at my CompletedEvent handler the iFrame is still empty.
The WebBrowser shows both web apps full loaded and working, so I suppose the handler is just not waiting for the iFrame content to load since it's done by a Ajax async call.
Any advice to acomplish it?
I think I explained this in the answer you linked (and in more details in another related answer). AJAX pages are non-determinustic, so there is no generic approach.
Use periodic asynchronous polling to watch the page's current HTML snapshot or DOM for changes, the linked post illustrates how to do that. You can poll the frame's content in the same way.
I can imagine the frame reports to be ready but it actually does not. For instance, the frame contains frames, you have no way to know whether all these frames are loaded by using DocumentCompleted event.
In short: Using a frame to load external stuff and do the testing is not a good approach. Even if you use a timer to check the loading status manually. But according to security considerations, you will have many problems to access the DOM.
I have two suggestions for you:
Create a WebBrowser instance and open external test subject into it. You will have a very good chance to know, whether document (and its frames) have been loaded completely. You still have the full control to access any elements of the WebBrowser or cookies or click elements or change elements.
Use 3rd tool such as Selenium as test driver.
Update 1:
As the questioner does not want any 3rd tool, I'd suggest let the internal test subject query the loading completeness of the target frame periodically. Possible code can be check document.readyState == 'complete'.
So far as I know, as the external test subject is embedded as frame, due to security consideration, you might not able to access the DOM of the frame. In other words, you cannot do mouse clicks, etc., unless you change the security settings for the Webbrowser control first.

How to programmatically click a button on a webpage in bot (web crawler)?

I would like to build a bot - web crawler - to collect phone numbers.
I have a problem though: to see the phone number, a user must click something like "Show".
How can I solve this problem?
Check what the act of clicking on the button does. Does it call a Javascript function? Does that make an HTTP call to a backend? If so your bot should do that call instead of screen-scraping the first page. If not, does it just play with the DOM of the page to show an item on screen?
All the data you're looking for comes from some sort of back-end, so if you look in the developer tools of your browser when going through the page you can usually figure out what calls to script in order to get the data.
It is possible to make this harder (and that is what some sites to to protect themselves from scraping). Typically if you're in this situation, what you're doing is not entirely legal or nice. But technically it's very interesting, so here goes.
The best way to go forward is to run the site in a real browser (like PhantomJS, or Chrome) and use a framework like Webdriver to simulate browser interactions. This way you can pull most of the data out usually.
If you find that your ip gets blocked, you may use Tor and use multiple instances dynamically to hit the site... but make sure you ask the site owner nicely if you're allowed to do that of course.

Update Windows Form Application UI from a Website [SCREEN-SCRAPING]

I have Windows Forms Application that Updates its GUI from a website using WebClient's GET Requests; However some of these values are updated in the web page using JavaScript so user don't have to keep refreshing the page to get them. How could i make my program get those values without having to keep sending new GET Requests.
The best way to do exactly what you want is to reverse engineer the javascript that updates the values on the page you're scraping. Beyond that, I'm afraid what you're doing is the best we can do.
On the plus side, javascript is nothing more than plain text source code so you can take a peek at it. But the legality of doing so depends on where you are. In most places including the US, just looking at the online code is legal. Reproducing it is not. But as the judge in the Oracle vs Google case said: it doesn't make sense to apply copyright to a single function (I'm paraphrasing, he said "range_check" not "a single function").
If the javascript is obfuscated then copy paste it into a pretty printer. Just Google for "javascript pretty printer". There are lots of them online.
You say that you want to be able to do something in C# like you do in JavaScript, but you don't want to have to "keep sending new GET Requests". The thing is, that's exactly what the JavaScript is doing. It just happens to be doing it asynchronously behind the scenes. You can do the very same thing with C#. The JavaScript is just doing GET or POST requests behind the scenes, and you can do the very same requests with C#.
Or you can simply set a timer to GetElementById from a hidden web browser

Categories