Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用now.js调用distributeMessage方法时出错_Node.js_Chat_Nowjs Sockets - Fatal编程技术网

Node.js 使用now.js调用distributeMessage方法时出错

Node.js 使用now.js调用distributeMessage方法时出错,node.js,chat,nowjs-sockets,Node.js,Chat,Nowjs Sockets,开始使用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,

开始使用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对象可用。因此,我不是简单地设置now.name,而是尝试设置now.name,并通过调用distributeMessage(“John已加入聊天室”)发送消息

Chrome和firefox报告一个错误

对象没有“distributeMessage”方法。

我不明白为什么。可以设置now.name属性。控制台日志显示具有get distributeMessage和set distributeMessage函数的对象。我可以在单击“发送按钮”元素时发送消息。但是,我现在无法调用distributeMessage


当我尝试进行方法调用时,now对象是否未完全加载?我是否需要包含某种“现在”onLoadReady方法?我需要做什么才能在提示后启动now.distributeMessage方法?

我找到了解决问题的方法。我需要在电话周围用一个

now.ready(function() {
  now.distributeMessage(now.name + " has joined chat.");
})

客户端now名称空间似乎可用,但所有服务器端now调用仍然需要javascript来完成处理。

我找到了一种方法来解决我的问题。我需要在电话周围用一个

now.ready(function() {
  now.distributeMessage(now.name + " has joined chat.");
})

客户端now命名空间似乎可用,但所有服务器端now调用仍然需要javascript来完成处理。

如果这仍然不能解决您的问题,最好更新您的问题,而不是自己回答。谢谢lig,使用now.ready确实解决了问题,从而回答了我的问题。我可以做些什么来让我的回答更清楚吗?我面临着同样的问题,但它仍然不能解决我的问题。如果这仍然不能解决你的问题,最好更新你的问题,而不是自己回答。谢谢lig,使用now.ready解决了问题,从而回答了我的问题。我能做些什么让我的回答更清楚吗?我面临着同样的问题,但它仍然不能解决我的问题。
now.ready(function() {
  now.distributeMessage(now.name + " has joined chat.");
})