# @sven0706/websocket

> 对websocket进行简单封装

Latest version **1.0.1** (published 2020-06-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install @sven0706/websocket
pnpm add @sven0706/websocket
yarn add @sven0706/websocket
bun add @sven0706/websocket
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-06-18 |
| First published | 2020-06-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Maintainers | sven0706 |

## Links

- npm: https://www.npmjs.com/package/@sven0706/websocket
- Repository: https://github.com/H-Sven/websocketJs
- Homepage: https://github.com/H-Sven/websocketJs#readme
- Issues: https://github.com/H-Sven/websocketJs/issues
- npm.io page: https://npm.io/package/@sven0706/websocket

## Recent versions

- 1.0.1 (latest) — 2020-06-18
- 1.0.0 — 2020-06-17

## README

# 对websocket进行简单封装

### 下载
```
npm install @sven0706/websocket -S
```

### 使用
```

// 在main.js或需要使用的地方引入并建立连接
import { createSocket } from '@sven0706/websocket'
createSocket('wss://api.baidu.com')


// 发送消息
import { sendWSPush } from '@sven0706/websocket'
sendWSPush(data)

// 接收消息
window.addEventListener('onmessageWS', getsocketData) // 注册监听事件
const getsocketData = e => { // 创建接收消息函数
  const data = e && e.detail.data
  console.log(data)
}
// 在需要的时候卸载监听事件，比如离开页面
window.removeEventListener('onmessageWS', getsocketData)

```

### API
```
仅三个api, 且一般只需要用到`createSocket`, `sendWSPush`

/**
 * 建立websocket连接
 * @param {string} url ws地址
 */
createSocket(url)

/**
 * 发送数据
 * @param {any} message 需要发送的数据
 */
 sendWSPush(message)

/**发送心跳
 * @param {number} time 心跳间隔毫秒 默认5000
 * @param {string} ping 心跳名称 默认字符串ping
 */
 sendPing()
```

---
_Source: https://npm.io/package/@sven0706/websocket · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
