更新记录
1.0.0(2025-09-30)
目前只适配调试了iOS,android 版本后续跟进。
平台兼容性
uni-app
Vue2 |
Vue3 |
Chrome |
Safari |
app-vue |
app-nvue |
Android |
iOS |
鸿蒙 |
× |
× |
× |
× |
× |
× |
× |
- |
× |
微信小程序 |
支付宝小程序 |
抖音小程序 |
百度小程序 |
快手小程序 |
京东小程序 |
鸿蒙元服务 |
QQ小程序 |
飞书小程序 |
快应用-华为 |
快应用-联盟 |
× |
× |
× |
× |
× |
× |
× |
× |
× |
× |
× |
uni-app x(4.76)
Chrome |
Safari |
Android |
iOS |
iOS插件版本 |
鸿蒙 |
微信小程序 |
× |
× |
- |
12 |
1.0.0 |
× |
× |
easy-socketio
开发文档
支持 Android/iOS 的 Socket.IO 客户端插件,适用于 uni-app x 平台。
iOS已经调试,可参照例子使用。Android版本代码已经加了,但未调试慎用。
功能
- connect(url)
- on(event, callback)
- emit(event, message)
- disconnect()
基础用法
//在一个uts文件里声明,比如:/store/imStore.uts。
import { SocketIOManager, SocketOptions } from "@/uni_modules/easy-socketio"
export type ChatStateType = {
socketConnected : boolean,
socket : SocketIOManager | null,
}
export const chatState = reactive({
socketConnected: false,
socket: null
} as ChatStateType)
export const setSocket = (socket : SocketIOManager) => {
chatState.socket = socket;
}
export const getSocket = () : SocketIOManager | null => {
return chatState.socket;
}
export const setSocketConnected = (connected : boolean) => {
chatState.socketConnected = connected;
}
export const getSocketConnected = () : boolean => {
return chatState.socketConnected;
}
@KeepAlive
export const initSocketIO = () => {
const SocketIO_URL = "http://192.168.1.237:5005";
const userId = "abc123";
if (userId == null || userId == "") {
console.log("User has not login!");
return;
}
const jsonObj = new UTSJSONObject()
jsonObj["uid"] = userId
let options : SocketOptions = {
params: JSON.stringify(jsonObj)
};
const socketIO = new SocketIOManager(SocketIO_URL, options);
setSocket(socketIO); 此处可以缓存起来,其他地方调用监听接口
socketIO.on('connect', () => {
console.log('SocketIO连接成功');
setSocketConnected(true)
});
socketIO.on('disconnect', () => {
console.log('SocketIO连接断开');
setSocketConnected(false)
});
socketIO.connect();
}
//在main.uts里调用初始化
import App from './App.uvue'
import { createSSRApp } from 'vue'
import { initSocketIO } from "@/store/imStore";
export function createApp() {
initSocketIO()
const app = createSSRApp(App)
return {
app
}
}
//im.uvue
<script>
import { getSocket, getSocketConnected} from "@/store/imStore"
export default {
methods: {
loadData(){
const socketIO = getSocket();
console.log('socketIO:', socketIO);
if (socketIO == null) {
return;
}
if (getSocketConnected()) {
socketIO.emit("friend_list", "");
//todo 各种emit初始化
} else {
socketIO.on('connect', () => {
console.log('In im page: SocketIO连接成功');
socketIO.emit("friend_list", "");
//todo 各种emit初始化
});
}
socketIO.on('friend_list', (data ?: any) => {
console.log('好友列表:', data);
//todo 收到的监听的数据
}
},
//发送消息的例子
sendMessage(msg: string){
console.log(message)
const socket = getSocket()
const msgObj = new UTSJSONObject()
msgObj["uid"] = "abc123"
msgObj["name"] = "nick"
msgObj["message"] = msg
const nowDate = new Date()
const nowYear = nowDate.getFullYear()
const nowMonth = nowDate.getMonth() + 1
const nowDay = nowDate.getDate()
const nowHour = nowDate.getHours()
const nowMinute = nowDate.getMinutes()
const nowSecond = nowDate.getSeconds()
msgObj["time"] = nowYear + "-" + this.repairZero(nowMonth) + "-" + this.repairZero(nowDay) + " " + this.repairZero(nowHour) + ":" + this.repairZero(nowMinute) + ":" + this.repairZero(nowSecond)
socket?.emit("send_message", JSON.stringify(msgObj))
}
}
}
</script>