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

@parkyang/mp-socket.io-client

v1.0.1

Published

适用于小程序的socket.io封装,可用于支付宝小程序、微信小程序

Downloads

4

Readme

@parkyang/mp-socket.io-client

仅支持 Server 端 Socket.io 2.x

项目源自:@hyoga/uni-socket.io version:1.0.0

原项目不支持支付宝小程序

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

介绍

重写 socket.io-client 的 engin.io-client 处理件,h5 依旧使用原生 WebSocket,APP 与小程序使用 uni-app 的 WebSocket 协议,所以 h5 端任然可以支持长轮询等方式,APP 与小程序只能支持 WebSocket 协议。

安装

// 建议使用npm或yarn包形式引入以保证插件的更新迭代
npm i @parkyang/mp-socket.io-client --save
// yarn add @parkyang/mp-socket.io-client

使用

import io from '@parkyang/mp-socket.io-client';

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请求,导致报错。