1.0.3 • Published 1 year ago
websocket-tool v1.0.3
websocket-tool
A 'WebSocket' tool class based on TypeScript encapsulation, which is small in size and convenient for quick use
一个基于TypeScript封装的 WebSocket
工具类,体积小,方便快速使用
如何使用呢?(How to use it?)
1、npm安装依赖(to install)
npm install websocket-tool
2、使用(reference)(下面例子是基于vue3项目来举例)
<script setup lang="ts">
import { onMounted } from 'vue';
import webSocketTool from 'websocket-tool';
onMounted(() => {
/*
webSocketTool.createInstance(url)
url 服务器的地址
*/
webSocketTool.createInstance('ws://localhost:8080')
const sendContent = JSON.stringify({content: '测试webSocket'})
/*
webSocketTool.send(sendContent,cb)
sendContent 发送给服务器的数据
cb 回调函数,接收服务器传回的数据
*/
webSocketTool.send(sendContent, (res: any) => {
console.log(res);
})
})
</script>