javascript - 向所有登录 Web 应用程序的用户播放实时通知声音,无需重新加载页面

标签 javascript c# jquery asp.net vb.net

我知道在网上可以找到很多关于我的问题的解决方案,但没有一个对我有用。这就是我问这个问题的原因。

首先让我解释一下我想要实现的目标 -

-> 我正在开发一个多用户 Web 应用程序 [ASP.Net] -> 我正在使用 SignalR 获取实时数据库更改通知,并且 SignalR 立即将更改通知传输给登录应用程序的所有用户。 -> 现在我想做的是为所有登录的用户播放通知声音,以便他们了解新的通知需要注意。

这就是我到目前为止所做的 -

JavaScript

<script>
    function playSound(mysound) {
        //thisSound = document.getElementById(mysound);
        //thisSound.Play();
        var audio = new Audio(mysound);
        audio.play();
    }

</script>

代码隐藏 -

ScriptManager.RegisterClientScriptBlock(Me, [GetType](), "OpenWindow", "javascript: playSound('./audio/notification.wav')", True)

这个解决方案的问题在于,用户需要重新加载页面才能听到通知声音,我认为如果用户不能像 SignalR 处理通知那样立即听到通知声音,那么这是毫无意义的。

是否可以将声音推送给所有客户端,这样他们就不需要重新加载页面?

任何帮助将不胜感激。如果您需要任何进一步的说明,请告诉我。

最佳答案

终于我成功了。我不得不稍微改变一下 jquery -

<script type="text/javascript">
        function playSound(mysound) {
            //thisSound = document.getElementById(mysound);
            //thisSound.Play();
            var audio = new Audio(mysound);
            audio.play();                
        }

        $(function () {
            var notify = $.connection.notificationsHub;
            var audio;

            notify.client.displayNotification = function (s_Not, s_Path) {
                $("#newNot").html(s_Not);
                audio = new Audio(s_Path);
                var iLevel = "<%=System.Web.HttpContext.Current.Session("USER_LEVEL")%>";
                var i_OldNot = "<%=System.Web.HttpContext.Current.Session("NOT_COUNT")%>";
                if (iLevel == 2) {
                    //alert(i_OldNot  + " " + s_Not);
                    if (i_OldNot < i_Not) {
                        playSound("/audio/notification.wav");
                        //i_OldNot == Number(s_not);
                    }
                }
            };

            $.connection.hub.start();                                
        });
    </script>

在后面的代码中,我必须设置一个 session 变量来存储更新之前的最后通知的数量。如果先前和当前的通知数量高于 session 值,则播放通知声音 -

System.Web.HttpContext.Current.Session("NOT_COUNT")= i_LastNotCount

现在,无需重新加载页面即可播放声音。特别感谢 rdans,因为他的以下评论,我有了这个想法 -

Then in a comment you say:

I already have implemented SignalR. So it's not the problem at all.

This suggests to me that you probably don't understand what signalR is doing because the whole point of signalR is to push to the browser without having to post back or use an ajax call.

关于javascript - 向所有登录 Web 应用程序的用户播放实时通知声音,无需重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37759760/

相关文章:

javascript - 在javascript中查找第n个月之后的日期

javascript - Linnworks 的 AJAX 请求

javascript - $ ("html").scrollLeft(值);在 Chrome 中不工作

javascript - 如何获取键/值 JavaScript 对象的键

javascript - jQuery 使用 .nextAll() 背靠背显示相邻的 XML 兄弟节点

javascript - Haxe 3.2 javascript 包名称

c# - WPF TextBoxBase(System.Windows.Controls.Primitives) 获取文本

c# - 从 System.Linq 导入类型如何向其他类添加方法?

c# - 如何通过 WPF 的 XAML 中的样式应用超链接 TextDecorations 属性

javascript - jQuery 不返回新的数据属性