1.0.5 • Published 4 years ago

cjy-socket v1.0.5

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

socket

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

安装(yarn or npm)

$ yarn add cjy-socket

使用方法 (vue为例)

import Socket from 'cjy-socket';

const socket = new Socket('localhost:3000') // 参数为具体的socket地址

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>

API

事件名说明参数
sendSocketMessage发送socket消息Object {cmd: 'event', msg: 'hello,socket'}
addListener监听具体某个消息事件event,callback(data)
listenAll监听所有消息事件callback(data)
removeListener移除监听的某个事件event
removeAllListener移除监听的所有事件
closeSocket断开socket连接

git 地址

https://github.com/chen-junyi/socket-js 欢迎交流~

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago