@uone/u-socket

1.3.0 • Public • Published

socket

原生websocket封装的插件,可以监听与发送服务端约定好的所有事件。

推荐使用方法

import Socket from '@uone/u-socket';

const socket = new Scoket('localhost:3000')

socket.initSocket()

Vue.prototype.$socket = socket
<template>
    <button @click="sendMsg">{{ count }}</button>
</template>

<script>
export default {
    data() {
        return {
            count: 0
        }
    },
    created() {
        this.$socket.addListener('event1'), (data)=> { // 监听具体某个事件
            console.log(data)
        }
        this.$socket.listenAll(data => { // 监听具体所有事件
            console.log(data)
        })
    },
    methods: {
        sendMsg () {
            this.$socket.sendSocketMessage({ // 发送socket消息
                msg: 'hello, socket'
            })
        }
    },
    destoryed () {
        this.$socket.removeListener('event1') //移除监听的某个事件 
        this.$socket.removeAllListener() //移除监听的所有事件
        this.$socket.closeSocket() //断开socket连接
    }
}
</script>

Readme

Keywords

Package Sidebar

Install

npm i @uone/u-socket

Weekly Downloads

0

Version

1.3.0

License

ISC

Unpacked Size

15.6 kB

Total Files

5

Last publish

Collaborators

  • uone