123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import { ref, onMounted, onUnmounted } from 'vue';
- import { getConversationSync } from '@/api/common'
- // 配置悟空IM
- import {
- MessageText,
- Channel,
- WKSDK,
- ChannelTypePerson,
- // ChannelTypeGroup
- } from "wukongimjssdk"
- import Snackbar from '@/plugins/snackbar'
- import { useUserStore } from '@/store/user'
- const userStore = useUserStore()
- const ConnectStatus = {
- Disconnect: 0, // 断开连接
- Connected: 1, // 连接成功
- Connecting: 2, // 连接中
- ConnectFail: 3, // 连接错误
- ConnectKick: 4, // 连接被踢,服务器要求客户端断开(一般是账号在其他地方登录,被踢)
- }
- initDataSource()
- // api 接入
- function initDataSource () {
- // 最近会话数据源
- WKSDK.shared().config.provider.syncConversationsCallback = async () => {
- const res = await getConversationSync({ msg_count: 100 })
- return res
- }
- }
- export function initKey () {
- return new Promise((resolve) => {
- userStore.getChatKey().then(() => {
- resolve()
- }).catch((error) => {
- // 报错重连
- console.log(error.msg)
- setTimeout(() => {
- initKey()
- }, 3000)
- })
- })
- }
- export function initConnect () {
- const connected = ref(false)
- const conversationList = ref([])
- // 单机模式可以直接设置地址
- WKSDK.shared().config.addr = 'ws://' + userStore.IMConfig.wsUrl // 默认端口为5200
- // 认证信息
- WKSDK.shared().config.uid = userStore.IMConfig.uid // 用户uid(需要在悟空通讯端注册过)
- WKSDK.shared().config.token = userStore.IMConfig.token // 用户token (需要在悟空通讯端注册过)
- // 连接
- onMounted(() => {
- // 连接状态监听
- WKSDK.shared().connectManager.addConnectStatusListener(
- async (status, reasonCode) => {
- if (status === ConnectStatus.Connected) {
- console.log('连接成功')
- connected.value = true
- // 获取列表
- const res = await syncConversation()
- conversationList.value = [...res]
- } else {
- console.log('reasonCode', reasonCode)
- connected.value = false
- conversationList.value = []
- }
- }
- )
- // 消息发送状态监听
- WKSDK.shared().chatManager.addMessageStatusListener(statusListen)
- // 常规消息监听
- WKSDK.shared().chatManager.addMessageListener(messageListen)
-
- connect()
- })
- onUnmounted(() => {
- // 连接状态监听移除
- WKSDK.shared().connectManager.disconnect()
- // 消息发送状态监听移除
- WKSDK.shared().chatManager.removeMessageStatusListener(statusListen)
- // 常规消息监听移除
- WKSDK.shared().chatManager.removeMessageListener(messageListen)
- })
- return { connected, conversationList }
- }
- // 消息发送状态监听
- function statusListen (packet) {
- if (packet.reasonCode === 1) {
- // 发送成功
- } else {
- // 发送失败
- }
- }
- // 常规消息监听
- function messageListen (message) {
- console.log('收到消息', message)
- // message.content // 消息内容
- // message.channel // 消息频道
- // message.fromUID // 消息发送者
- }
- export function connect () {
- WKSDK.shared().connectManager.connect()
- }
- export function send (user, text) {
- // 例如发送文本消息hello给用户u10001
- const _text = new MessageText(text) // 文本消息
- WKSDK.shared().chatManager.send(_text, new Channel(user, ChannelTypePerson))
- // 例如发送文本消息hello给群频道g10001
- // WKSDK.shared().chatManager.send(_text, new Channel(user,ChannelTypeGroup))
- }
- // 同步最近会话
- export async function syncConversation () {
- return new Promise((resolve) => {
- WKSDK.shared().conversationManager.sync().then(res => {
- resolve(res)
- }).catch(error => {
- Snackbar.error(error.msg)
- })
- })
- }
|