Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@liqt/mocksocket

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@liqt/mocksocket

mock socket

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

原理

chokidar在监听到 mock/socket.js 文件改变时,重新获取文件import-fresh, 执行回调去起一个ws

使用

1 安装

npm install mocksocket -g

2 在工程下创建 mock 文件夹,在其内编写 socket.js 文件,注意返回的对象使用JSON.stringify()进行包裹

socket.js 文件格式即为普通的 commonjs 文件,底层使用 require 语法引入,需符合 commonjs 语法

module.exports = {
  port: 1234, // 端口号,会占用本地机器的此端口号进行ws
  sendHandler: (ws) => {
    setInterval(() => {
      ws.send(JSON.stringify({ name: 'dashixiong' }));
    }, 1000); // 每秒推送一次数据
  }, // 模拟数据的函数,传入ws参数,使用send向客户端发送数据,可在此函数内使用setTimeInterval来增加模拟的复杂性
};

3 执行脚本

mocksocket;

FAQs

Package last updated on 15 Jun 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc