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

react-socket-hook

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-socket-hook

![GitHub](https://img.shields.io/github/license/leihancn/react-socket-hook) ![David](https://img.shields.io/david/leihancn/react-socket-hook) ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/leihancn/react-socket-hook) ![GitH

  • 1.0.4
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React Socket Hook

GitHub David GitHub code size in bytes GitHub release (latest by date) npm type definitions GitHub package.json dynamic

使用
# install
yarn add react-socket-hook
基础用法
import useSocket from 'react-socket-hook'

const { send } = useSocket('wss://echo.websocket.org', {
  onReceived: (data: DataType) => {
    // handle data in here.
  }
})

// send some data
send('something')
声明
export declare type DataType = string | ArrayBufferLike | Blob | ArrayBufferView

export interface UseSocketOptions {
  protocols?: WebSocket['protocol']
  binaryType?: BinaryType
  /**
   * 自动连接 auto connect server when it is ready.
   * @default true
   */
  autoConnect?: boolean
  onConnected?: () => void
  onClosed?: () => void
  onReceived?: (data: DataType) => void
  onError?: (errEvent: Event) => void
}

export interface UseSocketReturn {
  /**
   * 主动调用来拉起连接。 connect server.
   * 如果当前已是连接状态,则会保持状态。 if it is connected, will keep state.
   */
  connect: () => void
  /**
   * 主动断开连接。 disconnect server.
   */
  close: () => void
  /**
   * 发送数据。send data to server.
   * @param data
   */
  send: (data: DataType) => Promise<void>
}

export declare type UseSocket = (
  url: string,
  options?: UseSocketOptions
) => UseSocketReturn
常用指令
# 开发 develop
yarn dev
# 打包 build package
yarn build
# 提交代码 commit code
yarn cz

Keywords

FAQs

Package last updated on 22 Jul 2021

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