我需要一个“Hello world!”示例,演示如何组合使用websocket
和ejabberd
。所以,这就是我所在的地方:
ejabberd
psi plus
客户端,我可以发送
给自己留言ejabberd
知道它支持websockets
Strophe.js
连接到ejabberd
这是问题清单:
ejabberd
配置的正确方法是什么
(我想,我安装了一个新版本的ejabberd及其版本
配置位于ejabberd.yml
)Apache
服务器(代理,重写等) - 在许多论坛中
我见过一些涉及这种操作的例子)Javascript
或C++
或Python
代码会是什么
匹配这个谜题,这样我就可以做一个简单的事情
connect
或send
消息好吧,我花了两天时间,完全徒劳无功。无论我有什么配置设置和我拥有的代码,我总是从FF得到一条消息:
Firefox can't establish connection to the server
at ws://localhost:5280/http-ws
而且,是的,我编辑了ejabberd.yml
,因此它有以下几行:
requests_handlers:
"http-ws/": ejabberd_http_ws
我还编辑了apache配置,所以我有:
ProxyRequests off
ProxyPass /http-ws/ http://localhost:5280/http-ws/
ProxyPassReverse /http-ws/ http://localhost:5280/http-ws/
RewriteEngine on
RewriteRule http-ws/ http://localhost:5280/http-ws/
确实,我重启了apache和ejabberd。在我的代码中,我有:
var SocketUrl = 'ws://localhost:5280/http-ws'
...other standard stuff
我想,我尝试了几十种*几十种组合,但它们都不起作用。所以,我需要一个逐步的指导来回答这三个问题:
ejabberd
Apache
JavaScript
)应如何显示修改
我现在有这个配置:
1. ejabberd.yml:
...
hosts:
- "localhost"
....
listen:
....
-
port: 5280
ip: "::"
module: ejabberd_http
request_handlers:
"/http-ws": ejabberd_http_ws
web_admin: true
http_poll: true
http_bind: true
....
acl:
admin:
user:
- "admin": "localhost"
access:
configure:
admin: allow
2. apache2.conf
<Directory />
AllowOverride All
Allow From All
</Directory>
<Directory /var/www/site>
AllowOverride All
Allow From All
</Directory>
3. apache2/sites-available/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/site
<Directory /var/www/site>
Options +Indexes +MultiViews
</Directory>
ProxyRequests off
AddDefaultCharset UTF-8
ProxyPass /http-ws/ http://127.0.0.1:5280/http-ws/
ProxyPassReverse /http-ws/ http://127.0.0.1:5280/http-ws/
RewriteEngine on
RewriteRule /http-ws/http://127.0.0.1:5280/http-ws/
...
最后这是我的代码:
window.onload = function(){
var SocketUrl = 'ws://localhost:5280/http-ws/';
var connection = null;
function onConnect(status){
if (status == Strophe.Status.CONNECTING) {
alert('Strophe is connecting.');
} else if (status == Strophe.Status.CONNFAIL) {
alert('Strophe failed to connect.');
} else if (status == Strophe.Status.DISCONNECTING) {
alert('Strophe is disconnecting.');
} else if (status == Strophe.Status.DISCONNECTED) {
alert('Strophe is disconnected.');
} else if (status == Strophe.Status.CONNECTED) {
alert('Strophe is connected.');
}
}
var button = document.getElementById("connect");
button.onclick = function(){
connection = new Strophe.Connection(SocketUrl);
var jid = document.getElementById("jid").value;
var pass = document.getElementById("pass").value;
connection.connect(jid, pass, onConnect);
}
}
因此,在运行此示例时,我总是收到这些警报:
Strophe is connecting
Strophe failed to connect
Strophe is disconnected
在控制台中,我看到了以下错误消息:
Firefox can't establish a connection to the server at ws://localhost:5280/http-ws/
Ejabberd log
这就是我在ejabberd
日志中所拥有的:
2015-08-20 12:52:40.558 [error] <0.448.0> CRASH REPORT Process <0.448.0> with 0 neighbours crashed with reason: call to undefined function ejabberd_http_ws:process([], {request,'GET',[<<"http-ws">>],[{nokey,<<>>}],{<<>>,<<>>},undefined,<<"en-US">>,<<>>,{{0,0,0,0,...},...},...})
2015-08-20 12:52:40.559 [error] <0.326.0> Supervisor ejabberd_http_sup had child undefined started with {ejabberd_http,start_link,undefined} at <0.448.0> exit with reason undef in context child_terminated
我想,我应该从日志中强调这一行:
call to undefined function ejabberd_http_ws
我尝试在module
的{{1}}部分下启用此模块,例如:
ejabberd.yml
如果documentation没有撒谎,我认为应该有效。但是,在这种情况下,我无法重新启动modules:
...
mod_http_ws: {}
并在其日志文件中现在看到此消息:
ejabberd
封装
这是我使用命令安装的 initialization was aborted because a module start failed
包的一些信息:
ejabberd
所以,$ apt-get -y install ejabberd
返回
$ dpkg -s ejabberd
Package: ejabberd
Status: install ok installed
Priority: optional
Section: net
Installed-Size: 6405
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Version: 14.07-4ubuntu1
Provides: xmpp-server
Depends: adduser, erlang-base (>= 1:15.b) | erlang-abi-17.0, erlang-asn1 (>= 1:17.3-dfsg), erlang-base (>= 1:17.3-dfsg) | erlang-base-hipe (>= 1:17.3-dfsg), erlang-crypto (>= 1:17.3-dfsg), erlang-inets (>= 1:17.3-dfsg), erlang-mnesia (>= 1:17.3-dfsg), erlang-odbc (>= 1:17.3-dfsg), erlang-public-key (>= 1:17.3-dfsg), erlang-ssl (>= 1:17.3-dfsg), erlang-syntax-tools (>= 1:17.3-dfsg), openssl, ucf, debconf (>= 0.5) | debconf-2.0, erlang-lager, erlang-jiffy, erlang-p1-cache-tab, erlang-p1-iconv, erlang-p1-mysql, erlang-p1-pam, erlang-p1-pgsql, erlang-p1-sip, erlang-p1-stringprep, erlang-p1-stun, erlang-p1-tls, erlang-p1-utils, erlang-p1-xml, erlang-p1-yaml, erlang-p1-zlib, erlang-xmlrpc
Recommends: ejabberd-contrib
Suggests: libunix-syslog-perl, imagemagick | graphicsmagick-imagemagick- compat
Conflicts: ejabberd-mod-shared-roster-ldap
Conffiles:
/etc/default/ejabberd 49352468a39da545ad13586af05877f9
/etc/ejabberd/inetrc a8de3c20d2ac990c8f319907b340d6a2
/etc/init.d/ejabberd 58aa53c895ae2250e80ea16769db0417
/etc/logrotate.d/ejabberd d2a0a63e544aabfb797f718471c6d173
/etc/ufw/applications.d/ejabberd abc263fcde39ff65e9b5722be81db8ff
Description: distributed, fault-tolerant Jabber/XMPP server written in Erlang
ejabberd is a distributed, fault-tolerant Jabber/XMPP server written in Erlang.
返回
$ service ejabberd status
答案 0 :(得分:4)
Ejabberd支持从15.03版开始的websockets - 这解决了上面的答案。
这与python或C ++无关 - 这个难题只是将Ejabberd配置为支持websockets,并可选择使用代理来处理websockets流量。
在ejabjjerd.yml部分下,模块:ejabberd_http add
request_handlers:
"/http-ws": ejabberd_http_ws
这将在端口5280:/ http-ws
上提供如果这不方便,您可以使用apache或nginx从端口80进行代理。
对于nginx,在虚拟主机中你需要这样的东西,其中JABBERIP是ejabberd的ip地址。这代理了从标准http端口80到ejabberd的websocket连接,连接配置为处理websockets。
location /http-ws/ {
proxy_pass http://JABBERIP:5280; // this is
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
你可能还需要在ejabberd.yml中调整websocket设置 - 我把它们放在文件的顶部..
websocket_ping_interval: 60
websocket_timeout: 86400
设置strophe超出了本答案的范围,但关键是设置连接以使用websockets,如果浏览器支持此功能。以下snippit来自angularjs应用程序 - 尽管你明白了...... SocketUrl是/ http-ws位置的公共位置,如上所示..这可能是:5280版本或代理到端口80的版本。
if (window.WebSocket && (passwd != 'undefined' && passwd != null)) {
$this.connection = new Strophe.Connection($this.SocketUrl);
$this.connection.connect(user_info.username+'@'+$this.jabberUrl, passwd, $this.onConnect.bind($this));
} else {
UserService.getJabberData().then(function (response) {
var jabber = response.data;
$this.connection = new Strophe.Connection($this.BOSHUrl);
$this.connection.attach(jabber.jid, jabber.sid, jabber.rid, $this.onConnect.bind($this));
});
}
还有用于python和其他语言的xmpp库 - 但如果您正在构建一个在Web浏览器中运行的应用程序,那么strophe是一个很好的选择。