<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>