Javascript+Pubnub聊天通知

Javascript + Pubnub Chat Notifications

本文关键字:通知 聊天 Javascript+Pubnub      更新时间:2023-10-25

当用户提交聊天时,我试图发出声音,其他人也会听到。这是我的代码:

Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>
<code>Chat Output</code>
<div id=box></div>
<div id=pubnub pub-key=demo sub-key=demo></div>
<script src=http://cdn.pubnub.com/pubnub-3.1.min.js></script>
<script>(function(){
    var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chatlllll';
        PUBNUB.subscribe({
            channel : channel,
            callback : function(text) { 
                box.innerHTML = 
                  (''+text).replace( /[<>]/g, '' ) + '<br>' +      box.innerHTML; 
            }
        });
    PUBNUB.bind( 'keyup', input, function(e) {
       (e.keyCode || e.charCode) === 13 && PUBNUB.publish({
           playsound('http://www.aphpsite.comuv.com/sound/chat.wav')
           channel : channel, 
           message : input.value, 
           x : (input.value='')
       });
   });
})();</script>

这就是我所拥有的。我在添加声音时遇到问题。这个脚本坏了。所以这些都不起作用。我希望有人能修好它。

谢谢。

您正在询问PubNub和一个在聊天消息到达/发送时具有声音效果的示例聊天应用程序。我已经更新了这个例子,并提供了一个额外的sound.jsJavaScriptHTML5lib,它将有助于播放声音效果。请注意,我获取了您的声音WAV文件,并将其转换为OGGMP3的文件格式,以提供跨浏览器兼容性。接下来,我将粘贴完整的和工作的源代码,用于接收消息时的声音效果聊天。根据源代码,我粘贴了您需要的URL资源,如sound.jsaudio文件。

试试现场直播-http://pubnub-demo.s3.amazonaws.com/chat-with-sounds/chat.html

参见源代码:

<div><input id=input placeholder=chat-here></div>
<code>Chat Output</code>
<div id=box></div>
<div id=pubnub pub-key=demo sub-key=demo></div>
<script src=http://cdn.pubnub.com/pubnub-3.1.min.js></script>
<script src=sound.js></script>
<script>(function(){
    var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chatlllll';
    PUBNUB.subscribe({
        channel : channel,
        callback : function(text) { 
            // PLAY SOUND HERE
            sounds.play('chat');
            // UPDATE TEXT OUTPUT HERE
            box.innerHTML = 
                (''+text).replace( /[<>]/g, '' ) +
                '<br>' +
                box.innerHTML; 
        }
    });
    PUBNUB.bind( 'keyup', input, function(e) {
       (e.keyCode || e.charCode) === 13 && PUBNUB.publish({
           channel : channel, 
           message : input.value, 
           x       : (input.value='')
       });
   });
})();</script>

在GitHub上下载源代码

https://github.com/pubnub/pubnub-api/tree/master/app-showcase/chat-with-sounds-单击链接访问PubNub GitHub存储库,其中包含声音聊天演示的源代码。