Is it possible to subscribe to a C # event using JavaScript?

I am working on a web application that should often query the server database and check if the client has data available.

Ideally, I would need to get a javascript function callback on the server in order to be able to call the javascript function when any new data is available in the database, instead of polling the server every 5 seconds.

Simplification, I will need to be able to call the method on the server and pass the js function as a parameter to the callback. I want to avoid the overhead of re-polling the server.

Can this be done using asp.net and ajax?

+3
source share
3

, , COMET, , , . WCF PollingDuplex, , javascript.

, , , . Silverlight, AJAX: http://www.silverlightshow.net/news/AJAX-Client-for-HTTP-Polling-Duplex-WCF-Channel-in-Microsoft-Silverlight-3-.aspx

UPDATE: , "omg do pollling"! , , . PollingDuplex, WCF, , , javascript.

, .

2:

, . , HTTP. , , - . , . , . . . , HTTP-- "" .

. , . , , 5 . Chatty, , 4.8s, . ( 5 ) , , /.

:

sl3duplex.js , JavaScript, HTTP PollingDuplexHttpBinding System.ServiceModel.PollingDuplex.dll

.HTM :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>AJAX client for HTTP polling duplex WCF channel in Microsoft Silverlight 3</title>

    <script src="sl3duplex.js"></script>

    <script src="pubsub.js"></script>

    <script language="javascript">
        var sl3duplex = org.janczuk.sl3duplex;
        var proxy = null;

        function addNotification(text) {
            var notifications = document.getElementById("notifications");
            notifications.value += text + "\n";
            notifications.scrollTop = notifications.scrollHeight;
        }

        function alignUIWithConnectionState(connected) {
            document.getElementById("topicName").disabled =
               document.getElementById("subscribeButton").disabled = connected ? "disabled" : null;
            document.getElementById("publishText").disabled = connected ? null : "disabled"            
        }

        function onSubscribe() {                        
            proxy = new sl3duplex.Sl3DuplexProxy({
                url: window.location.href.substring(0, window.location.href.lastIndexOf("/")) + "/PubSubService.svc",
                onMessageReceived: function(body) {
                    addNotification("SERVER NOTIFICATION: " + (new sl3duplex.NotificationMessage(body)).text);
                },
                onError: function(args) {
                    addNotification("ERROR: " + args.error.message);
                    alignUIWithConnectionState(false);
                }
            });

            alignUIWithConnectionState(true);

            var topic = document.getElementById("topicName").value;
            proxy.send({ message: new sl3duplex.SubscribeMessage(topic),
                         onSendSuccessful: function(args) {
                             addNotification("CLIENT ACTION: Subscribed to topic " + topic);
                         }
                       });
        }

        function onPublish(event) {
            if (event.keyCode == 13) {
                var publishText = document.getElementById("publishText");
                var content = publishText.value;
                publishText.value = "";
                var topic = document.getElementById("topicName").value;

                proxy.send({ message: new sl3duplex.PublishMessage(topic, content),
                             onSendSuccessful: function(args) {
                                 addNotification("CLIENT ACTION: Published to topic " + topic + ": " + content);
                             }
                           });                               
            }
        }
    </script>

</head>
<body bgcolor="Tomato">
    <table style="width: 640px; font-family: Arial, Helvetica, sans-serif; font-size: 11px;"
        cellspacing="2" align="center">
        <tr>
            <td colspan="2">
                Topic to subscribe and publish to:
            </td>
        </tr>
        <tr>
            <td style="width: 448px">
                <input id="topicName" type="text" value="Dante" style="width: 100%" />
            </td>
            <td style="width: 192px">
                <input id="subscribeButton" type="button" value="Subscribe" style="width: 100%" onclick="onSubscribe();" />
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td colspan=2>
                Notifications:
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <textarea id="notifications" name="S1" rows="18" readonly="readonly" style="width: 100%;
                    background-color: ButtonFace"></textarea>
            </td>
        </tr>
        <tr>
            <td colspan="2">
                &nbsp;
            </td>
        </tr>
        <tr>
            <td colspan="2">
                Enter text to publish and press Enter:
            </td>
        </tr>
        <tr>
            <td colspan="2">
                <input id="publishText" type="text" style="width: 100%" disabled="disabled" onkeypress="onPublish(event);" />
            </td>
        </tr>
    </table>
</body>
</html>

Silverlight . .JS, , " JavaScript, HTTP" silverlight.

+5

, - , GET ajax ( jQuery javascript), , ,

Ext.Ajax.request({
    url : 'http://yourdomain.com/controller/Action' , 
    params : { action : 'getDate' },
    method: 'GET',
    success: function ( result, request ) { 
        Ext.MessageBox.alert('Success', 'Data return from the server: '+ result.responseText); 
//.. do more view related stuff here , if this was a grid your would probably reload the store
    },
    failure: function ( result, request) { 
        Ext.MessageBox.alert('Failed', result.responseText); 
    } 
});

, ajax- .

,

+3

- - ..

AFAIK Microsoft WCF , SIlverlight javascript. , , .

- .

0
source

Source: https://habr.com/ru/post/1720079/


All Articles