Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
@hyoga/uni-socket.io
Advanced tools
Readme
重写 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
使用本插件,请注意插件版本与服务端 socket.io 版本要匹配,否则将会出现无法连接服务器的问题。
uni-socket 版本 | 服务端 socket.io 版本 |
---|---|
1.x | 2.x |
2.x | 3.x |
3.x | 4.x |
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
为什么没有聊天室示例代码?
本项目仅仅是将 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。
FAQs
适用于uni-app的socket.io封装,可用于uni-app、微信小程序
The npm package @hyoga/uni-socket.io receives a total of 409 weekly downloads. As such, @hyoga/uni-socket.io popularity was classified as not popular.
We found that @hyoga/uni-socket.io demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.