【问题标题】:now.js how to get 'now' object without browser (simple client)?now.js 如何在没有浏览器(简单客户端)的情况下获取“现在”对象?
【发布时间】:2012-08-09 06:51:49
【问题描述】:

我正在玩 node.js 和 now.js。一切正常。但我想做一个简单的客户端,我可以从命令行运行(所以没有浏览器)。

http://nowjs.com/doc/example

在示例中,提供了一个 HTML 页面,该页面包含 now.js 文件,该文件创建了神奇的“现在”对象。但是在命令行上没有这样的东西。

对于我正在运行的服务器 (helloworld_server.js)

还有我的客户端 helloworld_client.js:

// client.js
var nowjs = require("now");

// now i need to connect to the server (127.0.0.1:8080)
// so i i need a server object?

server = ????

var everyone = nowjs.initialize(server);

everyone.now.distributeMessage('hi!');

那么我如何获得'now'对象呢?

【问题讨论】:

  • 我不相信你可以用 nowjs 做到这一点。您可能需要查看 dnode。它与 nowjs 不同,但根据您的需要,它可能适合您。 github.com/substack/dnode

标签: node.js nowjs-sockets


【解决方案1】:

好的,知道了。一旦你现在安装

npm install now

它会创建一个 node_modules 文件夹,在里面你可以看到每个扩展的文件夹。更深入地发现:

./node_modules/now/examples

还有 nodeclient_example 文件夹

./node_modules/now/examples/nodeclient_example

从那里很清楚,但好奇的人,这就是你需要的魔法:

var nowjs = require('../../lib/nodeclient/now.js');
var now = nowjs.nowInitialize('http://localhost:8080');

它就是“魔法”现在对象

确保确定你安装:

npm install socket.io-client

否则它对我不起作用!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 2021-02-01
    • 2021-12-10
    • 1970-01-01
    • 2012-09-11
    • 2019-12-17
    相关资源
    最近更新 更多