从Node.js脚本发送XMPP通知 [英] Send XMPP notification from Node.js script

查看:127
本文介绍了从Node.js脚本发送XMPP通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node脚本如何通过XMPP向Jabber用户发送通知(例如通过Google Hangouts)?我看过像 xmpp / client 这样的库,但它们看起来有些过分。是否有更简单的解决方案?

How can a Node script send a notification via XMPP to a Jabber user (e.g. via Google Hangouts)? I've looked at libraries like xmpp/client but they seem overkill. Is there a simpler solution?

推荐答案

通过节点中的XMPP发送消息的最简单方法



可能没有其他更简单的节点XMPP客户端库,而不是 node-简单XMPP
在这种情况下,用于向另一个Jabber用户发送消息的最小Node.js脚本是:

Simplest way to send a message via XMPP in Node

There is probably no other simpler XMPP client library for Node than node-simple-xmpp. In this case the minimal Node.js script to send a message to another Jabber user is:

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
});

如果这两个帐户从未一起发言,则还需要初步订阅:

If the two account have never spoken together, a preliminary 'subscribe' is also required:

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 )。

As you can see in package.json node-simple-xmpp lib has a dependency on [node-xmpp-client] (https://github.com/xmppjs/xmpp.js/tree/node-xmpp/packages/node-xmpp-client).

上述脚本也可以使用Google Talk / Hangouts工作(测试),您只需要替换 xmpp.jp 服务器 talk.google.com 并使用Google帐户。启用 https://myaccount.google.com/lesssecureapps 以启用Node.js脚本以登录使用Google帐户。

The script above is working (tested) also with Google Talk/Hangouts, you just have to replace xmpp.jpserver with talk.google.com and use a Google account. Turn on https://myaccount.google.com/lesssecureapps to enable Node.js script to sign in with Google account.

https://npms.io/search?q=node-xmpp 还有一些其他的节点XMPP客户端库,但几乎所有它们依赖于 node-xmpp-client 或仅限于BOSH连接(通过HTTP轮询)。

As of https://npms.io/search?q=node-xmpp there are a few other XMPP Client libraries for Node, however almost all of them are dependent on node-xmpp-client or limited to BOSH connection (polling over HTTP).

一个有趣的lib用于客户端的 Strophe.js 似乎是 node-strophe 。它基于 Strophe.js 1.0.2版,该版本是适用于在任何浏览器中运行的应用程序的库。不幸的是,该版本除了BOSH之外不支持(参见 Strophe.js changelog ),websocket仅在1.1.0版本之后可用。

One interesting lib for those used to Strophe.js on client side seems 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 is available only since release 1.1.0.

没有特定XMPP库的替代解决方案可能是使用 Net模块,但在这种情况下,您需要管理所有XMPP交互以建立与服务器的连接,请参阅 https://wiki.xmpp.org / web / Programming_XMPP_Clients

An alternative solution without specific XMPP libraries could be using 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 module :

Below is a very raw example of script trying to initiate the connection with a Jabber server using Net module:

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

You can see in the console log the correct answer of Jabber server, however from then on it's a mess: you should begin exchanging TLS messages (see https://xmpp.org/rfcs/rfc3920.html#tls)

我认为唯一可行的选择是第一个使用 node-simple-xmpp 库。

I think the only feasible alternative is the first one using node-simple-xmpp library.

这篇关于从Node.js脚本发送XMPP通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆