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

发布于 2024-12-21 01:59:27 字数 1452 浏览 1 评论 0原文

开始使用 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?", "")

});

我已经让 node.js 服务器和 now.js 正常工作。

我正在尝试扩展聊天功能,以便当用户输入其姓名时,会向服务器发送一条消息,指出“现已加入聊天”。示例代码提示用户输入名称并将其设置为 now 对象的名称。

now.name = prompt("What's your name?", "");

此时,now对象就可用了。因此,我不是简单地设置 now.name,而是尝试设置 now.name 并通过调用 allocateMessage('John has join chat.') 发送消息。Chrome

var p = prompt("What's your name?", "");
if (!p) {
    // errs here
} else {
    now.name = p;
    now.distributeMessage(now.name + " has joined chat.");
}

和 Firefox 报告错误,内容为

Object #< ;对象>没有方法“distributeMessage”。

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

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

Started playing with the now.js framework. Using the example code in now.js I'm trying to implement a chat. Including here for completeness.

<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?", "")

});

I've got the node.js server with now.js working properly.

I'm trying to extend the chat so that when a user enters their name, a message is sent to the server noting that " has now joined the chat." The example code prompts a user for a name and sets that to the now object's name.

now.name = prompt("What's your name?", "");

At this point, the now object is available. So instead of simply setting the now.name, I'm trying to set the now.name AND send a message by calling distributeMessage('John has joined chat.')

var p = prompt("What's your name?", "");
if (!p) {
    // errs here
} else {
    now.name = p;
    now.distributeMessage(now.name + " has joined chat.");
}

Chrome and firefox report an error that reads

Object #<Object> has no method 'distributeMessage'.

I don't understand why. The now.name property can be set. The console log shows the object with get distributeMessage and set distributeMessage functions. I can send the message when I click on the 'send-button' element. But, I am unable to call the distributeMessage at this point.

Is the now object not fully loaded when I try to make the method call? Do I need to include some sort of 'now' onLoadReady method? What do I need to do in order to fire off the now.distributeMessage method after the prompt?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-12-28 01:59:27

我找到了解决我的问题的方法。我需要用一个来包围分发消息的调用,

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

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

I've found a method to address my issue. I need to surround the call to distribute message with a

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

It seems the client side now namespace was available but all serverside now calls still required javascript to finish processing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文