所以,从现在开始的2天我一直在努力开发。问题是:我有一台使用XMPP协议来处理聊天的服务器。我必须连接它,但我正在开发一个Phonegap / Cordova应用程序,而且没有插件。
我曾尝试使用Strophe.js连接到服务器,但没有成功。服务器要求我使用普通身份验证,并且已经为我提供了编码密码。
示例代码:
$scope.onConnect = function(status){
if (status == Strophe.Status.CONNECTING) {
$scope.connStatus = 'Strophe is connecting.';
} else if (status == Strophe.Status.CONNFAIL) {
$scope.connStatus = 'Strophe failed to connect.';
} else if (status == Strophe.Status.DISCONNECTING) {
$scope.connStatus = 'Strophe is disconnecting.';
} else if (status == Strophe.Status.DISCONNECTED) {
$scope.connStatus = 'Strophe is disconnected.';
} else if (status == Strophe.Status.CONNECTED) {
$scope.connStatus = 'Strophe is connected.';
}
}
$scope.chatConnect = function(ID, xmppToken){
connection = new Strophe.Connection('chat.server.com');
Strophe.SASLPlain.priority = 99;
Strophe.SASLAnonymous.test = function() {
return false;
};
Strophe.SASLMD5.test = function() {
return false;
};
Strophe.SASLSHA1.test = function() {
return false;
};
connection.rawInput = function(data){alert("Input: " + data);};
connection.rawOutput = function(data){alert("Output: " + data);};
connection.connect(ID, xmppToken, $scope.onConnect);
}
我也欢迎新图书馆的建议! 提前谢谢。
答案 0 :(得分:2)
以下是部署在Plunker上的Ionic聊天应用程序的一个非常简单的示例(需要部署为localhost的XMPP服务器):http://plnkr.co/edit/i3at7UvgqHaiL8NzN2k3
因此,在您的控制器中,代码可以是:
$scope.usr = {username: 'hello@test.com', password: 'pippo', connected: false};
var server = 'test.com'; // adapt to your server config (domain)
var xmpp_server = 'http://127.0.0.1:7070/http-bind/';
var connection = null;
$scope.connect = function (usr) {
connection = new Strophe.Connection(xmpp_server);
connection.connect(usr.username, usr.password, onConnect);
}
其他具有Strophe.js用法示例的掠夺者: