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

mp-websocket-polyfill

v0.3.0

Published

Let WebSocket in WeChat miniprogram to adapt browser's lib.

Downloads

6

Readme

mp-websocket-polyfill

适用于微信小程序的垫片,使小程序的 SocketTask 兼容浏览器端的 WebSocket,这样便可以使用 Stomp.js 等库。

安装

yarn add mp-websocket-polyfill

# 小程序真机调试,可能会因找不到 TextEncoder、TextDecoder 而报错
# 所以,推荐安装垫片:
yarn add fastestsmallesttextencoderdecoder

使用方式

解决真机 TextEncoderTextDecoder 找不到问题

安装依赖:

yarn add fastestsmallesttextencoderdecoder

然后在入口处:

import 'fastestsmallesttextencoderdecoder'

直接使用

原来:

const socketTask = wx.connectSocket({
  url: 'localhost:8080/ws',
  // ...其他参数
})

替换为:

import Ws from 'mp-websocket-polyfill'

const webSocket = new Ws({
  url: 'localhost:8080/ws',
  // ...其他参数,和 wx.connectSocket 保持一致
})

然后这个 webSocket 对象便可以在只支持浏览器端 WebSocket 的地方使用了。

配合 Stomp.js 使用

import { Client } from '@stomp/stompjs'
import Ws from 'mp-websocket-polyfill'

const client = new Client({
  // ↓ 请使用 webSocketFactory 修改默认的 websocket 构造器
  webSocketFactory() {
    return new Ws({
      url: 'ws://localhost:8080/gs-guide-websocket',
      protocols: ['v12.stomp', 'v11.stomp', 'v10.stomp'], // ← 这是 stomp 协议的默认写法,可供参考
    })
  },
})

client.activate()

注意事项

注意事项:

  • 使用垫片构造的实例,原先小程序自带的 API 例如 onOpenonClose 均被屏蔽无法使用,但是可以通过新实例 .socketTask 属性获取小程序原生的 SocketTask 对象;
  • 如果使用 Stomp,建议参考上面的示例,有时 protocols 也要配置正确。

API

  • new Ws(options):兼容微信小程序的 wx.connectSocket() 的参数,提供额外的配置字段:
    • fixIOSWechat0x00Issue:默认为 trueboolean 类型),是否修复 iOS 微信接收到 Stomp 消息时可能因遗漏末尾 0x00 字符而导致解析失败的问题;
  • ws.socketTask:通过实例的这个属性,可以获取原生的小程序 socketTask 对象。