使用now.js调用distributeMessage方法时发生错误
开始玩now.js框架。 使用now.js中的示例代码,我试图实现聊天。 包括这里的完整性。
<script src="http://localhost:8080/nowjs/now.js" type="text/javascript"></script> <!-- HTML for Chat Here --> $(document).ready(function() { now.receiveMessage = function(name, message) { $(".chattext").append("<br>" + name + ": " + message); $(".chattext").attr({ scrollTop: $(".chattext").attr("scrollHeight") }); } $("#send-button").click(function() { now.distributeMessage($("#text-input").val()); $("#text-input").val(""); }); now.name = prompt("What's your name?", "") });
我已经得到了与now.js正常工作的node.js服务器。
我试图扩展聊天,以便当用户input他们的名字时,会向服务器发送一条消息,指出“现在已经join了聊天”。 示例代码提示用户input名称,并将其设置为now对象的名称。
now.name = prompt("What's your name?", "");
在这一点上,现在的对象是可用的。 所以,而不是简单地设置now.name,我试图设置now.name并通过调用distributeMessage('John已join聊天')发送消息。
var p = prompt("What's your name?", ""); if (!p) { // errs here } else { now.name = p; now.distributeMessage(now.name + " has joined chat."); }
Chrome和Firefox会报告读取错误
对象#<对象>没有方法'distributeMessage'。
我不明白为什么。 now.name属性可以设置。 控制台日志通过获取distributeMessage显示对象并设置distributeMessage函数。 当我点击“发送button”元素时,我可以发送消息。 但是,我现在无法调用distributeMessage。
当我尝试进行方法调用时,现在的对象是不是完全加载? 我是否需要包含某种“now”onLoadReady方法? 在提示符之后为了激发now.distributeMessage方法,我需要做些什么?
我find了解决我的问题的方法。 我需要围绕呼叫分配消息与
now.ready(function() { now.distributeMessage(now.name + " has joined chat."); })
似乎客户端现在命名空间是可用的,但所有的服务器现在调用仍然需要JavaScript来完成处理。