My question is.. how am I able to send 2 data parameters to the browser within my C# application? I have a command line application which is a server for my game and on a certain message from the client I have to activate JavaScript on the webpage the client is playing on. What do I use to achieve this?
1 Answer
Wow is this a wide-open question. If the request from the browser is for a full HTML page, then embed the call to the JavaScript function within an inline script tag on the page. If the request from the browser will be via ajax, it's totally up to you how you send that data, but JSON is a common mechanism.
6 Comments
Fabian Pas
Well, I'm not sure how to program the code to send a certain message to the browser and process it with Javascript. Well the JS part is easy. It's just how to get the data from my server to the browser. Do I use websockets or node.js?
T.J. Crowder
@FabianPas: You don't necessarily need to use either. Use any server-side technology you like (C# is fine). On the client, use ajax (directly or via a library). For instance, with jQuery you could use
ajax and return a response with Content-Type set to JSON and the response body could be {param1: "value1", param2: "value2"}. Search on JSON and ajax/XMLHttpRequest for more info.Fabian Pas
But how do I send to AJAX with C#?
T.J. Crowder
@FabianPas: Have you done any research? A bit of searching should give you several examples to work from. You initiate the request from the browser (search for "ajax"). You respond to the request in the normal way (a simple HTTP response) with the appropriate
Content-Type header.Fabian Pas
Well, thank you! It's just the basic principle that doesn't come to me, haha. But I'll try!
|