使用 Javascript 连接到 XMPP 服务器
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 服务器):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:
所以,从现在开始这两天我一直在努力开发这个。问题是:我有一台使用 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 服务器):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: