0.1.4 • Published 2 years ago

multiple-websockets v0.1.4

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

multiple-websockets FOSSA Status

a React Component or Hook which allow multiple websocket connections.

  • Always keep one communication when repeatly open the same websocket url
  • Allow multiple communication when open the different urls
  • Supports multiple pages/components processing the communication with the same url

Install

$ npm i multiple-websockets --save
or
$ yarn add multiple-websockets

Example

import { Component,useEffect } from "react";
import { WebSocket as ws, useWebsocket } from "multiple-websockets";

const wsUrl = "ws://localhost:8001";
class Com1 extends Component {
  //open callback
  openCB = (e) => {};
  //message callback
  messageCB = (e) => {};
  //close callback
  closeCB = (e) => {};
  //error callback
  errorCB = (e) => {};

  open = () => {
    ws.open(wsUrl, 5, this.openCB, this.messageCB, this.closeCB, this.errorCB);
  };
  send = () => {
    ws.sendMessage(wsUrl, "test123");
  };
  close = () => {
    ws.close(wsUrl);
  };

  render() {
    return (
      <>
        <button onClick={this.open}>open</button>
        <button onClick={this.send}>send</button>
        <button onClick={this.close}>close</button>
      </>
    );
  }
}

function Com2() {
  const [message, open, send, close] = useWebsocket(wsUrl, 5);
  useEffect(() => {
    // message
  }, [message]);
  return (
    <>
      <button onClick={open}>open</button>
      <button onClick={send}>send</button>
      <button onClick={close}>close</button>
    </>
  );
}

function App() {
  return (
    <>
      <h1>class</h1>
      <Com1 />
      <h1>hook</h1>
      <Com2 />
    </>
  );
}

Lisctens

FOSSA Status

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

0.1.2

2 years ago

1.0.10

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago