简体   繁体   中英

Can we call a javascript function from WebService?

I have a situation where I have to call aa javascript method from SQL Server. I haven't found any solution to do the same. I found one thing that I can call web service from SQL Server but now the problem is "Can we call javascript function from web service (like we can do in code behind file of aspx page)"?

Please help

Well, it depends on what you mean. If I'm not mistaken the guys behind the free real-time communication platform, xSockets have been able to use a compiled stored procedure in SQL Server to use xSockets to dispatch a call to connected clients (eg using WebSockets). I'm not saying this is something you should do , I'm saying "I think they have done it". You will find contact info on their page or just tweet with #xsockets hash on Twitter and they will respond.

You could do this using Server-Sent Events , which are supported in most recent browsers.

The PHP:

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

echo "id: " . time() . " " . PHP_EOL;
echo "data: $message" . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();

The JS:

var source = new EventSource('/php');
source.onmessage = function(e) {
    console.log(e.data);
};

You can "call" javascript directly from the webservice... Or more correctly spoken you can use a publish/subscribe pattern to publish a message from you webservice and subscribe to it in your javascript.

Like @Daniel says we (XSockets) have called javascript from a compiled stored procedure, but that was a few years ago... I think it will be easier for you to take the approach where you "call" the javascript from a webservice.

To do it between webservice and javascript could be something like this... JAVASCRIPT

//Setup a subscription for the event 'foo'
conn.on('foo', function(data){
    console.log('foo - some data arrived from the webservice', data);
});

C# (or VB if you want to)

//Connect to the realtime server and the default controller generic
var conn = ClientPool.GetInstance("ws://127.0.0.1:4502/Generic", "*");
//We send an anonymous message here, but any object will work.
conn.Send(new { Message = "Hello JavaScript from C#" },"foo");

Full example here

There may be some easier way to do this as well... if I knew a little bit more about what you are doing :)

不,您不能从服务器上的Web服务调用客户端上的javascript函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM