vue 项目使用Strophe.js连接XMPP服务器Openfire

论坛 期权论坛 脚本     
匿名技术用户   2021-1-7 14:33   60   0

https://blog.csdn.net/xiao__gui/article/details/42642573 参考部分 Strophe.js使用

https://blog.csdn.net/love254443233/article/details/7751785 参考部分 xxml消息订阅

mian.js文件中 引入,注册成全局变量 ,此后调用 this.STROPHE.(方法名)

import strophe from './strophe/stropheUA'

Vue.prototype.STROPHE = strophe

stropheUA.js

// XMPP服务器BOSH地址
var BOSH_SERVICE = 'http://x.x.x.x:7070/http-bind';
// XMPP连接
var connection = null;
// 当前状态是否连接
var connected = false;
// 存放添加好友请求
var addFriend_warn = []


// 登陆
function login(username, password) {
 if (!connected) {
  connection = new Strophe.Connection(BOSH_SERVICE);
  connection.connect(username + '@openfire.cn', password, onConnect);
 }
}
// 接收到<message>
function onMessage(msg) {
 // 解析出<message>的from、type属性,以及body子元素
 var from = msg.getAttribute('from').match(/(\S*)@/)[1];
 // var from = msg.getAttribute('from');
 var type = msg.getAttribute('type');
 var elems = msg.getElementsByTagName('body');
 if (type == "chat" && elems.length > 0) {
  var body = elems[0];
  // 文本
  var text = Strophe.getText(body)
  if (text.search('data:image') !== -1) {
   // base64图片
  }
  // 可使用jq获取id插入元素 或者使用vue监听由消息则添加
 }
 return true;
}

// 发送
function sendMsg(otherId, myselfId, message) {
 if (connected) {
  var msg = $msg({
   to: otherId + '@openfire.cn',
   from: myselfId + '@openfire.cn',
   type: 'chat',
   xmlns: "jabber:client"
  }).c("body", null, message);
  connection.send(msg.tree());
  connection.send($pres().tree());
 }
}

// 发送前需要将图片转换成bese64
function sendImg(otherId, myselfId, message) {
 if (connected) {
  var msg = $msg({
   to: otherId + '@openfire.cn',
   from: myselfId + '@openfire.cn',
   type: 'chat',
   xmlns: "jabber:client"
  }).c("body", null, message);
  connection.send(msg.tree());
  connection.send($pres().tree());
 }
}


function onConnect(status) {
 console.log(status)
 console.log(Strophe.Status)
 if (status == Strophe.Status.CONNFAIL) {
  alert("连接失败!");
 } else if (status == Strophe.Status.AUTHFAIL) {
  alert("登录失败!");
 } else if (status == Strophe.Status.DISCONNECTED) {
  alert("连接断开!");
  connected = false;
 } else if (status == Strophe.Status.CONNECTED) {
  console.log('连接成功')
  connected = true;
  // 当接收到<message>节,调用onMessage回调函数
  connection.addHandler(onMessage, null, 'message', null, null, null);
  connection.addHandler(on_subscription_request, null, "presence", "subscribe");
  // 首先要发送一个<presence>给服务器(initial presence)
  connection.send($pres().tree());
 }
}

// 查询好友列表
function findFriends() {
 var friends = [];
 var iq = $iq({ type: 'get' }).c('query', { xmlns: 'jabber:iq:roster' });
 connection.sendIQ(iq, function (a) {
  $(a).find('item').each(function () {
   var jid = $(this).attr('jid').match(/(\S*)@/)[1];
   friends.push(jid)
  });
 });
 return friends
}

// 添加好友
function getFriend(name) {
 connection.send($pres({ to: name + '@openfire.cn', type: "subscribe" }));
}

// 收到好友请求
function on_subscription_request(stanza) {
 if (stanza.getAttribute("type") == "subscribe") {
  var from = stanza.getAttribute("from").match(/(\S*)@/)[1]
  addFriend_warn.push(from)
 }
 addFriend_warn = Array.from(new Set(addFriend_warn));
}

// 接受邀请
function on_subscription_response(name) {
 connection.send($pres({ to: name + '@openfire.cn', type: "subscribed" }));
}
// 拒绝邀请
function on_subscription_reject(name) {
 connection.send($pres({ to: name + '@openfire.cn', type: "unsubscribed" }));
}
// 删除好友
function on_subscription_delete(name) {
 connection.send($pres({ to: name + '@openfire.cn', type: "unsubscribe" }));
}

export default {
 login,
 sendMsg,
 sendImg,
 findFriends,
 getFriend,
 on_subscription_response,
 on_subscription_request,
 on_subscription_reject,
 addFriend_warn,
 on_subscription_delete
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP