npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@hyoga/uni-socket.io

v3.0.4

Published

适用于uni-app的socket.io封装,可用于uni-app、微信小程序

Downloads

1,041

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

常见问题

  1. 为什么没有聊天室示例代码?

    本项目仅仅是将 socket.io 封装到 uni-app 使用,并非完整的聊天室。

  2. Exception: ReferenceError: Can't find variable: window

    hbuilder x 2.6.3 版本中 v3 编译有 bug,升级 hbuilder x 即可。

  3. 真机运行 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请求,导致报错。

  4. 无法连接服务端? 如果是真机调试,请确保设备与服务端在同一个局域网内,此外注意,Socket.io 以及升级到 3.x,请注意版本匹配。Socket.io 3.x 请使用 @hyoga/uni-socket 2.x,Socket.io 2.x 请使用 uni-socket 1.x。

联系作者

如果该项目对您有帮助,可以给作者一个star

  • qq 群
  • 公众号,欢迎关注,不定时更新

前端方程式