1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<template>
<div class="test">
nim-test
<input v-model="info"/>
<button @click="onPost">发送</button>
</div>
</template>
<script>
import NIM from '@yxim/nim-web-sdk/dist/SDK/NIM_Web_NIM.js'
import { showNotify } from 'vant'
export default {
data() {
return {
nim: null,
// 是否已连接
isConnect: false
}
},
computed: {
accountId() {
return this.$route.query.accountId
}
},
async created() {
this.isConnect = false
this.nim = NIM.getInstance({
debug: true,
appKey: '6c51376a55f54b2fa586d7b4c85757f8',
account: this.accountId,
token: '123456',
onconnect: function() {
console.log('连接成功 ================>')
this.isConnect = true
},
onwillreconnect: function(obj) {
console.log('即将重连')
console.log(obj.retryCount)
console.log(obj.duration)
},
ondisconnect: function(error) {
showNotify({ type: 'warning', message: '连接失败', duration: 0 })
// if (error) {
// switch (error.code) {
// case 302:
// console.warn('账号或者密码错误')
// break
// }
// }
},
onmsg: function (msg) {
console.log('收到新消息===========>', msg);
}
});
},
methods: {
async onPost() {
let msg = this.nim.sendText({
scene: 'p2p',
to: '18987175004',
text: 'hello',
done: function sendMsgDone (error, msg) {
console.log('sendText', error, msg)
}
})
console.log('发送的消息===========>', msg)
}
},
beforeUnmount() {
if (this.nim) {
this.nim.destroy({
done: function (err) {
console.log('nim 断开连接')
}
})
}
}
}
</script>
<style lang="less" scoped>
</style>