Sign in to follow this  
xsirxx

[web] ASP.NET 2.0, C#, AJAX. How to send a client a string?

Recommended Posts

First post here in a while, and Im just moving into playing around with web programming, so thanks for listening! Now here is my problem :) : Im currently programming in ASP.NET 2.0 with AJAX(JScript client-side) and C#(server-side). I currently have the client able from jscript to call up a server-side function and serialize/deserialize the string using JSON and custom converters. My problem now comes in where I want the server to call the client side by sending a string(or any object) without the client initiating the conversation. I know I can have a timer on the client side go off and then do a callback to the server-side function, but I dont want a timer always going off. So right now I have created a custom control extending System.Web.UI.WebControls.CompositeControl, IScriptControl, and ICallbackEventHandler. I have a registered client script block using Page.ClientScript.RegisterClientScriptBlock(...) that was help built using Page.ClientScript.GetCallbackEventReference(...) - which all works great. So final question is, how do I call up a client-side function from the server-side without using a timer on client to initiate conversation? Thanks much and if anyone has any information or another forum recommendation to post on I would GREATLY appreciate it! -Brad

Share this post


Link to post
Share on other sites
What about

//It executes the code after the page is done loading

this.UnLoad +=
new EventHandler(
delegate{
HttpContext.Current.Response.Write(@"
<script type='text/javascript'> calljavascriptClientCode(); </script>");
});

//Or

//Execute the code before the any asp.net page is executed

//Create a new Global.asax file for your web application and add the following //code to it

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
Page page = HttpContext.Current.CurrentHandler as Page;
if(page != null) {
HttpContext.Current.Response.Write(@"
<script type='text/javascript'> calljavascriptClientCode(); </script>");
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by xsirxx
So final question is, how do I call up a client-side function from the server-side without using a timer on client to initiate conversation?


Think about what you're asking. Typically, a web client requests a page from a server, which returns it, and then the transaction is complete. What you're wanting is for the server to then contact the client again... but how? That web client may no longer exist (the browser closed), the computer may not be switched on, it may now be at a different address or share that address with many others (internet vagaries). The web isn't designed to do what you want to do.

Now, there is a sort of hack for doing this, these days called Comet, but you really don't want to go down this route without first understanding why it is the wrong view to take in 99% of cases. Web systems should fundamentally be about the client requesting things from a server. That is what the system is designed for and that is also what the user expects. If you want long-running bidirectional connections, that is where something like Java or Flash comes in, or even non-web protocols.

Share this post


Link to post
Share on other sites
Thank you for the replys guys!

kylotan:
Yea I realize that, but some types of applications would be better off if it had bi-directional. For instance a chat application. If a chat application had to constantly ask the server every couple seconds for a response, it would be a huge waste of bandwidth. I just assumed that there would be a way to do this given the strength of the web now....

rpgmaker:
Those methods are really just one time events basically still called by the client right? Or can those events be fired off by the server when it wants? If so if you would be so kind as to throw a link my way? Right now I dont really have any post backs running except the first or of course on a refresh. I am using all call-backs which leads to almost 0 visible notices of a refresh. Then I have the client calling the server through a timer-callback and then I shoot back an array of JSON objects which are really my events to update the client.... But if I can take that timer out and somehow fire off a HttpResponse without a request, well that would be awesome!

Thanks!


Share this post


Link to post
Share on other sites
Quote:
Original post by xsirxx
Yea I realize that, but some types of applications would be better off if it had bi-directional. For instance a chat application. If a chat application had to constantly ask the server every couple seconds for a response, it would be a huge waste of bandwidth. I just assumed that there would be a way to do this given the strength of the web now....


Well, no, because that's not how the web works. Sorry. :) Clients call servers, not the other way around. If you want other stuff, typically you step outside of HTTP and the WWW.

Share this post


Link to post
Share on other sites
I wasnt saying it should, I was just saying that I thought there would have been a way. I think the web is really growing beyond what it was initially built for. Also I noticed on wikipedia that HTTP5 supports Comet, or Comet like bi-directional talking.

Thanks for your help!
--Brad

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this