import ObjTree from 'objtree'
import btoa from 'btoa'
data() {
return {
socketTaskId: ''
}
},
methods: {
openSocket() {
// 第一先建立连接
let host= 'wss://10.11.0.100:7443/ws'
var socketTask = uni.connectSocket({
url: host,
protocols: ['xmpp'],
success: e => {
console.log('@@@@@@@@connectSocketsuccess', e)
},
fail: e => {
console.log('@@@@@@@@connectSocketfail', e)
}
});
// 监听到连接成功之后
socketTask.onOpen(data => {
// 第一次连接成功后发起第一个流
that.startOpenSocket('10.11.0.100', 'test@10.11.0.100')
})
socketTask.onMessage(data => {
// 将XML转换为json对象
const xml2json = xotree.parseXML(data.data) || {}
if (xml2json.hasOwnProperty('open')) {
// 储存第一次返回的id,当第二个流成功之后,再第三个流使用
this.socketTaskId = xml2json.open['-id']
}
if (xml2json['stream:features'] && xml2json['stream:features'].mechanisms) {
// 第二个流: (第一个流返回stream:features之后发送第二个流 ,带上账号密码)
this.sendSocketUserInfo()
}
if (xml2json.hasOwnProperty('success')) {
// 第三个流 (带上id)
this.sendSocketId()
}
if (xml2json['stream:features'] && xml2json['stream:features'].bind) {
// 第四个流 (bind 绑定操作)
this.sendSocketBind()
}
if (xml2json.iq && xml2json.iq.bind && xml2json.iq['-type']) {
// 第五个流 (获取session)
this.getSocketSession()
}
if (xml2json.iq && !xml2json.iq.bind && xml2json.iq['-type']) {
// 第六个流 (获取session之后发起上线请求)
this.sendOnlineRequest()
}
if (xml2json.presence && xml2json.presence.status === 'online') {
// 上线成功 ...
}
})
},
startOpenSocket() {
let temp = {
'open': {
'-to': '10.11.0.100',
'-from': 'test@10.11.0.100',
'-xmlns': 'urn:ietf:params:xml:ns:xmpp-framing',
'-xml:lang': 'zh',
'-version': '1.0',
}
};
this.handleSendSocket(temp)
},
sendSocketUserInfo() {
let xotree = new ObjTree()
/**
* from => test@10.11.0.100 (username + '@' + to)
* 分隔 => String.fromCharCode(0)
* temp => from + 分隔 + 账号 + 分隔 + 密码
*/
let temp = `test@10.11.0.100${String.fromCharCode(0)}test${String.fromCharCode(0)}test`
// Base64编码
let token = btoa(temp);
let message = {
"auth": {
"-xmlns": "urn:ietf:params:xml:ns:xmpp-sasl",
"-mechanism": "PLAIN",
"#text": token
}
};
// json转XML
let temp = xotree.writeXML(message)
uni.sendSocketMessage({
data: temp
});
},
sendSocketId() {
var temp = {
"open": {
'-to': '10.11.0.100',
'-from': 'test@10.11.0.100',
"-xmlns": "jabber:client",
"-version": '1.0',
"-id": this.socketTaskId,
"-xml:lang": 'zh'
}
};
this.handleSendSocket(temp)
},
sendSocketBind() {
var temp = {
"iq": {
"-id": this.socketTaskId,
"-type": "set",
"bind": {
"-xmlns": "urn:ietf:params:xml:ns:xmpp-bind",
"resource": '小程序' // 此处是自定义的类似标识,增长客户端的描述
}
}
};
this.handleSendSocket(temp)
},
getSocketSession() {
var temp = {
"iq": {
"-xmlns": "jabber:client",
"-id": this.socketTaskId,
"-type": "set",
"session": {"-xmlns": "urn:ietf:params:xml:ns:xmpp-session"}
}
};
this.handleSendSocket(temp)
},
sendOnlineRequest() {
var temp = {
"presence": {
"-id": this.socketTaskId,
"status": "online",
"priority": "1"
}
};
this.handleSendSocket(temp)
},
handleSendSocket(temp) {
let xotree = new ObjTree()
let steam = xotree.writeXML(temp);
uni.sendSocketMessage({
data: steam
});
}
}
OpenFire webSocket uni-app wx小程序之用户登录
于 2022-01-11 16:26:13 首次发布