@hyoga/uni-socket.io
v3.0.4
Published
适用于uni-app的socket.io封装,可用于uni-app、微信小程序
Downloads
1,041
Maintainers
Readme
@hyoga/uni-socket
项目源自:weapp.socket.io,该项目作者已经两年没有维护,出现 bug 无法修复。
最近需要在 uni-app 中用到 socket.io,遇到 bug 没有人修复很是头疼,所以基于 weapp.socket.io 新起一个项目,已支持 Socket.io 3.x。
介绍
Socket.io 官方套件已升级至 3.x,本插件内置 socket.io 已同步升级至 3.x,使用 2.x 版本的@hyoga/uni-socket 请确保服务端使用 3.x 的 socket.io。
重写 socket.io-client 的 engin.io-client 处理件,h5 依旧使用原生 WebSocket,APP 与小程序使用 uni-app 的 WebSocket 协议,所以 h5 端任然可以支持长轮询等方式,APP 与小程序只能支持 WebSocket 协议。
安装
// 建议使用npm或yarn包形式引入以保证插件的更新迭代
npm i @hyoga/uni-socket.io --save
// yarn add @hyoga/uni-socket.io
使用
import io from '@hyoga/uni-socket.io';
const socket = io('your websocket path', {
query: {},
transports: [ 'websocket', 'polling' ],
timeout: 5000,
});
socket.on('connect', () => {
// ws连接已建立,此时可以进行socket.io的事件监听或者数据发送操作
// 连接建立后,本插件的功能已完成,接下来的操作参考socket.io官方客户端文档即可
console.log('ws 已连接');
// socket.io 唯一连接id,可以监控这个id实现点对点通讯
const { id } = socket;
socket.on(id, (message) => {
// 收到服务器推送的消息,可以跟进自身业务进行操作
console.log('ws 收到服务器消息:', message);
});
// 主动向服务器发送数据
socket.emit('send_data', {
time: +new Date(),
});
});
socket.on('error', (msg: any) => {
console.log('ws error', msg);
});
更多使用方法,请参考socket.io-client写法即可。
API
参考官网 API
常见问题
为什么没有聊天室示例代码?
本项目仅仅是将 socket.io 封装到 uni-app 使用,并非完整的聊天室。
Exception: ReferenceError: Can't find variable: window
hbuilder x 2.6.3 版本中 v3 编译有 bug,升级 hbuilder x 即可。
真机运行 TypeError: undefined is not an object (evaluating 'document.createElement')?
示例代码中:const socket = io('your websocket path', { query: {}, transports: [ 'websocket', 'polling' ], timeout: 5000, });
不要漏写
transports: [ 'websocket', 'polling' ]
,如果没有指定协议,貌似 socket.io 会默认走JSONP Polling
请求,导致报错。无法连接服务端? 如果是真机调试,请确保设备与服务端在同一个局域网内,此外注意,Socket.io 以及升级到 3.x,请注意版本匹配。Socket.io 3.x 请使用 @hyoga/uni-socket 2.x,Socket.io 2.x 请使用 uni-socket 1.x。
联系作者
如果该项目对您有帮助,可以给作者一个star。
- qq 群
- 公众号,欢迎关注,不定时更新