Test.vue 2.52 KB
<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>