【问题标题】:Javascript + Pubnub Chat NotificationsJavascript + Pubnub 聊天通知
【发布时间】:2012-07-17 23:38:32
【问题描述】:

我试图在用户提交聊天时发出声音,对方也会听到。这是我的代码:

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>

这就是我所拥有的。我无法添加声音。这个脚本坏了。所以这些都不起作用。我想知道是否有人可以修复它。

谢谢。

【问题讨论】:

    标签: javascript chat pubnub


    【解决方案1】:

    您询问的是 PubNub 和一个在聊天消息到达/发送时具有音效的示例聊天应用程序。我更新了示例并提供了一个额外的 sound.js JavaScript HTML5 库,这将有助于播放声音效果。请注意,我将您的声音 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 存储库,其中包含 Chat with Sound 演示的源代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-23
      相关资源
      最近更新 更多