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
86
87
<template>
<div class="test">
nim-test
<input v-model="info"/>
<button @click="onPost">发送</button>
</div>
</template>
<script>
import SDK from '@yxim/nim-web-sdk'
export default {
data() {
return {
nim: null
}
},
computed: {
accountId() {
return this.$route.query.accountId
}
},
async created() {
// const nim = NIM.getInstance({
// appkey: "6c51376a55f54b2fa586d7b4c85757f8",
// debugLevel: "debug",
// apiVersion: "v2"
// })
// if (!this.accountId) {
// console.log('缺少账号信息')
// return
// }
// try {
// const token = '123456'
// await nim.V2NIMLoginService.login(this.accountId, token, {
// "forceMode": false
// })
// console.log('登录成功')
// this.nim = nim
// nim.V2NIMMessageService.on("onReceiveMessages", function (messages) {
// console.log('监听消息==============================>')
// console.log(messages)
// })
// } catch (err) {
// // TODO failed, check code
// console.log(err)
// }
this.nim = SDK.NIM.getInstance({
debug: true,
appKey: '6c51376a55f54b2fa586d7b4c85757f8',
account: this.accountId,
token: '123456',
onmsg: function (msg) {
console.log('收到新消息', msg);
}
});
},
methods: {
async onPost() {
// try {
// // 创建一条文本消息
// const message = this.nim.V2NIMMessageCreator.createTextMessage("hello world")
// console.log('message==============================>')
// console.log(message)
// // 发送消息
// const res = await this.nim.V2NIMMessageService.sendMessage(message, '18487350810|1|18987175004')
// } catch (err) {
// // todo error
// console.log('onPost', err)
// }
let msg = this.nim.sendText({
scene: 'p2p',
to: '18987175004',
text: 'hello',
done: function sendMsgDone (error, msg) {
console.log('sendText', error, msg)
}
});
}
}
}
</script>
<style lang="less" scoped>
</style>