[英]Connecting to XMPP server with Javascript
因此,我從現在開始兩天一直在嘗試開發。 問題是:我有一台使用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);
}
我也歡迎新圖書館的建議! 提前致謝。
這是在Plunker上部署的Ionic聊天應用程序的一個非常簡單的示例(需要將XMPP服務器部署為localhost): 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用法示例的Plunker:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.