/    Sign up×
Community /Pin to ProfileBookmark

How can server interrupt client in browser?

I have a Javascript program which runs in the browser and has
functions work(), and stop().
It listens to commands from the server to work() and can be
interrupted by the server to stop().
I am using XmlHttpRequest to talk to the server.
So I use http GET to send a command “ready” to the server, which
replies at some point in time by sending “work” which is invoked by
the callback.

As per my understanding, the browser client is single threaded. How to
get the server to interrupt the browser client to say “stop”?

I modified Flanagan’s example code here:

[code]
var HTTP = {

// This is a list of XMLHttpRequest creation factory functions to try
_factories : [
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject(“Msxml2.XMLHTTP”); },
function() { return new ActiveXObject(“Microsoft.XMLHTTP”); }
],

// When we find a factory that works, store it here
_factory : null,

// Create and return a new XMLHttpRequest object.
//
// The first time we’re called, try the list of factory functions
until
// we find one that returns a nonnull value and does not throw an
// exception. Once we find a working factory, remember it for later
use.
//
newRequest : function() {
if (HTTP._factory != null) return HTTP._factory();

for(var i = 0; i < HTTP._factories.length; i++) {
try {
var factory = HTTP._factories[i];
var request = factory();
if (request != null) {
HTTP._factory = factory;
return request;
}
}
catch(e) {
continue;
}
}

// If we get here, none of the factory candidates succeeded,
// so throw an exception now and for all future calls.
HTTP._factory = function() {
throw new Error(“XMLHttpRequest not supported”);
}
HTTP._factory(); // Throw an error
},

/**
* Use XMLHttpRequest to fetch the contents of the specified URL
using
* an HTTP GET request. When the response arrives, pass it (as plain
* text) to the specified callback function.
*
* This function does not block and has no return value.
*/
toServer : function(url) {
var request = HTTP.newRequest();
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200)
MyProxy.fromServer(request.responseText);
}
request.open(“GET”, url, true); // 3rd param implies asynchronous
console.log(“sending ” + url);
request.send(null);
},

};

…..
// MyProxy

fromServer: function(command) {
console.log(“fromServer: ” + command);
eval(command);
},
[/code]

thanks,
Anil

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @AnilPhilip spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...