2012-07-17 130 views
1

我试图在用户提交聊天时发出声音,也会在其他人的声音中听到。这里是我的代码:Javascript + Pubnub聊天室通知

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> 

这就是我所拥有的。我无法添加声音。该脚本已损坏。所以这些都不起作用。我想如果有人能解决它。

谢谢。

回答

3

您在聊天消息到达/发送时询问PubNub和具有音效的示例聊天应用程序。我更新了示例并提供了额外的sound.js JavaScript HTML5 lib,它将有助于播放音效。请注意,我将您的Sound WAV文件转换为OGGMP3文件格式以提供跨浏览器兼容性。接下来,我将在接收消息时粘贴完整和工作源代码,以便与音效聊天。在源代码之后,我粘贴了您需要的URL资源,例如sound.js音频文件。

试试吧LIVE! - http://pubnub-demo.s3.amazonaws.com/chat-with-sounds/chat.html

查看源代码:GitHub上

<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> 

下载源代码

https://github.com/pubnub/pubnub-api/tree/master/app-showcase/chat-with-sounds - 点击链接访问PubNub GitHub的库源代码与声音演示聊天。

+0

非常感谢。 – Djmann1013 2012-07-18 13:47:39