GithubHelp home page GithubHelp logo

geoffzhu / fake-websocket Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 26 KB

fake websocket & server in browser

JavaScript 79.48% HTML 20.52%
websocket websocket-simulation websocket-emulation fake-websocket-api

fake-websocket's Introduction

Fake-websocket

websocket-simulation in browser

Install

npm i fake-websocket

Usage

Simulation ws server

let server = new FakeWebSocket.WebSocketServer('ws://127.0.0.1:8080');
server.on('connection', (ws) => {
  ws.on('message', (e) => {
    console.log('server get message', e.data);
    ws.send({ b: 2 });
  });
});

Simulation ws client

import { WebSocket } from 'fake-websocket';

let ws = new WebSocket('ws://127.0.0.1:8080');

ws.onmessage = (e) => {
  console.log('client get message', e.data);
};

ws.onopen = (e) => {
  ws.send({ a: 1 });
}

ws.onerror = (e) => {
  console.error(e);
}

Why

  • e2e测试,替换浏览器环境中WebSocket对象,实现Websocket本地模拟,无需启动server跑真实socket服务
  • 一些逆向工作需要,例如微信开发者工具中,appservice(逻辑层),pageframe(渲染层)和native交互采用websocket,替换后可浏览器内模拟并替换通信消息

fake-websocket's People

Contributors

geoffzhu avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

fake-websocket's Issues

Cannot read property 'emit' of undefined

Closing a fake websocket seems to throw an error. The eventTarget is the mocked websocket. Calling close on this throws the error.

Uncaught TypeError: Cannot read property 'emit' of undefined
    at Array.WebSocketServer.clients.close (VM2190 index.js:132)
    at EventTarget.close (VM2190 index.js:54)
    at eval (VM2206 use-websocket.ts:582)
    at HTMLUnknownElement.callCallback (VM2200 react-dom.development.js:3945)
    at Object.invokeGuardedCallbackDev (VM2200 react-dom.development.js:3994)
    at invokeGuardedCallback (VM2200 react-dom.development.js:4056)
    at flushPassiveEffectsImpl (VM2200 react-dom.development.js:23538)
    at unstable_runWithPriority (VM2202 scheduler.development.js:468)
    at runWithPriority$1 (VM2200 react-dom.development.js:11276)
    at flushPassiveEffects (VM2200 react-dom.development.js:23442)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.