从 Node.js 脚本发送 XMPP 通知
Send XMPP notification from Node.js script
Node 脚本如何通过 XMPP 向 Jabber 用户发送通知(例如通过 Google Hangouts)?我看过像 xmpp/client 这样的库,但它们似乎有点过分了。有更简单的解决方案吗?
在节点中通过 XMPP 发送消息的最简单方法
可能没有比 node-simple-xmpp 更简单的 Node XMPP 客户端库了。
在这种情况下,向另一个 Jabber 用户发送消息的最小 Node.js 脚本是:
var xmpp = require('simple-xmpp');
var jid = 'testjs@xmpp.jp';
var pwd = 'xyz';
var server = 'xmpp.jp';
var port = 5222;
xmpp.on('online', function(data) {
console.log('Connected with JID: ' + data.jid.user);
xmpp.send('testjs@007jabber.com', 'hello! time is '+new Date(), false);
});
xmpp.on('error', function(err) {
console.error("error:", JSON.stringify(err));
});
xmpp.connect({
jid: jid,
password: pwd,
host: server,
port: port
});
如果两个帐号从来没有一起说过话,还需要一个初步的'subscribe':
xmpp.subscribe('testjs@007jabber.com');
正如您在 package.json node-simple-xmpp 中看到的那样,lib 依赖于 [node-xmpp-client] (https://github.com/xmppjs/xmpp.js/tree/node-xmpp/packages/node-xmpp-client)。
与Google的用法Talk/Hangouts
上面的脚本也适用于(测试过)Google Talk/Hangouts,您只需将 xmpp.jp
server 替换为 talk.google.com
并使用 Google 帐户。打开 https://myaccount.google.com/lesssecureapps 以启用 Node.js 脚本以使用 Google 帐户登录。
其他 XMPP 库
从 https://npms.io/search?q=node-xmpp 开始,还有一些用于 Node 的其他 XMPP 客户端库,但是几乎所有这些库都依赖于 node-xmpp-client 或仅限于 BOSH连接(通过 HTTP 轮询)。
对于那些习惯于在客户端 Strophe.js 的人来说,一个有趣的库似乎 node-strophe. It is based on Strophe.js release 1.0.2 which is a library for applications that run in any browser. Unfortunately that version didn't support other than BOSH (see Strophe.js changelog),websocket 仅在 1.1.0 版本后可用。
探索没有特定 XMPP 库的替代方案
没有特定 XMPP 库的替代解决方案可以使用 Net module, but in this case you need to manage all XMPP interactions to establish the connection to the server, see https://wiki.xmpp.org/web/Programming_XMPP_Clients。
下面是一个非常原始的脚本示例,尝试使用 Net 模块:
启动与 Jabber 服务器的连接
var net = require('net');
var jid = 'testjs@xmpp.jp';
var pwd = 'xyz';
var server = 'xmpp.jp';
var port = 5222;
var msg = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="'+server+'">';
var client = new net.Socket();
client.connect(port, server, function() {
console.log('Connected');
client.write(msg);
});
client.on('data', function(data) {
console.log('Received: ' + data);
});
您可以在控制台日志中看到 Jabber 服务器的正确答案,但是从那时起就一团糟:您应该开始交换 TLS 消息(参见 https://xmpp.org/rfcs/rfc3920.html#tls)
结论
我认为唯一可行的选择是第一个使用 node-simple-xmpp 库的方法。
Node 脚本如何通过 XMPP 向 Jabber 用户发送通知(例如通过 Google Hangouts)?我看过像 xmpp/client 这样的库,但它们似乎有点过分了。有更简单的解决方案吗?
在节点中通过 XMPP 发送消息的最简单方法
可能没有比 node-simple-xmpp 更简单的 Node XMPP 客户端库了。 在这种情况下,向另一个 Jabber 用户发送消息的最小 Node.js 脚本是:
var xmpp = require('simple-xmpp');
var jid = 'testjs@xmpp.jp';
var pwd = 'xyz';
var server = 'xmpp.jp';
var port = 5222;
xmpp.on('online', function(data) {
console.log('Connected with JID: ' + data.jid.user);
xmpp.send('testjs@007jabber.com', 'hello! time is '+new Date(), false);
});
xmpp.on('error', function(err) {
console.error("error:", JSON.stringify(err));
});
xmpp.connect({
jid: jid,
password: pwd,
host: server,
port: port
});
如果两个帐号从来没有一起说过话,还需要一个初步的'subscribe':
xmpp.subscribe('testjs@007jabber.com');
正如您在 package.json node-simple-xmpp 中看到的那样,lib 依赖于 [node-xmpp-client] (https://github.com/xmppjs/xmpp.js/tree/node-xmpp/packages/node-xmpp-client)。
与Google的用法Talk/Hangouts
上面的脚本也适用于(测试过)Google Talk/Hangouts,您只需将 xmpp.jp
server 替换为 talk.google.com
并使用 Google 帐户。打开 https://myaccount.google.com/lesssecureapps 以启用 Node.js 脚本以使用 Google 帐户登录。
其他 XMPP 库
从 https://npms.io/search?q=node-xmpp 开始,还有一些用于 Node 的其他 XMPP 客户端库,但是几乎所有这些库都依赖于 node-xmpp-client 或仅限于 BOSH连接(通过 HTTP 轮询)。
对于那些习惯于在客户端 Strophe.js 的人来说,一个有趣的库似乎 node-strophe. It is based on Strophe.js release 1.0.2 which is a library for applications that run in any browser. Unfortunately that version didn't support other than BOSH (see Strophe.js changelog),websocket 仅在 1.1.0 版本后可用。
探索没有特定 XMPP 库的替代方案
没有特定 XMPP 库的替代解决方案可以使用 Net module, but in this case you need to manage all XMPP interactions to establish the connection to the server, see https://wiki.xmpp.org/web/Programming_XMPP_Clients。
下面是一个非常原始的脚本示例,尝试使用 Net 模块:
启动与 Jabber 服务器的连接var net = require('net');
var jid = 'testjs@xmpp.jp';
var pwd = 'xyz';
var server = 'xmpp.jp';
var port = 5222;
var msg = '<stream:stream xmlns="jabber:client" xmlns:stream="http://etherx.jabber.org/streams" version="1.0" to="'+server+'">';
var client = new net.Socket();
client.connect(port, server, function() {
console.log('Connected');
client.write(msg);
});
client.on('data', function(data) {
console.log('Received: ' + data);
});
您可以在控制台日志中看到 Jabber 服务器的正确答案,但是从那时起就一团糟:您应该开始交换 TLS 消息(参见 https://xmpp.org/rfcs/rfc3920.html#tls)
结论
我认为唯一可行的选择是第一个使用 node-simple-xmpp 库的方法。