简体   繁体   English

Socketstream(0.3)服务器端代码混乱

[英]Socketstream(0.3) server-side code confusion

I'm trying to understand exactly where/how I should implement node.js/socketstream server side code that runs independent of client rpc calls. 我试图确切地了解应该在哪里/如何实现独立于客户端rpc调用运行的node.js / socketstream服务器端代码。 As a simple example I'm trying to push a regular clock update to connected clients using something like this on the server side: 作为一个简单的示例,我试图在服务器端使用类似以下的方法向连接的客户端推送常规时钟更新:

var pushTime = function() {
    d = new Date();
    ss.publish.all('newServerTime', d);
    return;
};

setInterval(pushTime, 1000);

And setting up the client to subscribe to that publish event sorta like this: 并设置客户端以订阅该发布事件sorta,如下所示:

ss.event.on('newServerTime', function(time) {
    return $('#serverTime').val(time);
});

Problem: where do I put/execute the server side pushTime function? 问题:我在哪里放置/执行服务器端pushTime函数? The docs suggest the /server/rpc tree so I put it in /server/rpc/demo.js but that yields this error: 文档建议使用/ server / rpc树,因此我将其放在/server/rpc/demo.js中,但这会产生此错误:

ReferenceError: ss is not defined ReferenceError:未定义ss

Mind you, I'm not putting the code in the export.actions block; 请注意,我没有将代码放在export.actions块中; I believe that's only for client rpc calls. 我相信这仅适用于客户端rpc调用。

I tried setting ss at the top of the file: 我尝试在文件顶部设置ss:

ss = require('socketstream');

but that's gotta be wrong - now the 'publish.all' method doesn't exist. 但这一定是错误的-现在'publish.all'方法不存在。

I tried putting the code at the bottom of app.js, right after the ss.start call. 我尝试将代码放在ss.start调用之后的app.js底部。 Again that says the publish.all method doesn't exist (maybe not until there's a client attached?). 再说一遍,publish.all方法不存在(也许直到附加了一个客户端才可以?)。 I'm lost. 我迷路了。 Any help appreciated; 任何帮助表示赞赏; hope this was clear. 希望这很清楚。

Yup, you could put that code in your actions, nothing to stop you, but better to put it in your 'app.js' file. 是的,您可以将代码放入您的操作中,没有什么可以阻止您,但是最好将其放入您的“ app.js”文件中。

To access the internal API from app.js (the one that's sent through to /server/rpc action files) use ss.api 要从app.js(发送到/ server / rpc操作文件的内部API)访问内部API,请使用ss.api

Hence you will need to call: 因此,您将需要致电:

ss.api.publish.all()

from your 'app.js' file. 从您的“ app.js”文件中。

Apologies this wasn't documented before. 抱歉,以前没有记录。 I will update the docs shortly. 我将尽快更新文档。

Owen 欧文

Don't know if it complies to the coding standards, but this probably works: 不知道它是否符合编码标准,但这可能有效:

/server/rpc/demo.js /server/rpc/demo.js

exports.actions = function(req, res, ss) {
    setTimeout(function () {
        ss.publish.all("newServerTime", new Date());
    }, 1000);
}

When reading the docs I think you can abuse actions for pretty much everything, not just RPC responses. 阅读文档时,我认为您几乎可以滥用所有操作,而不仅仅是RPC响应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM