网页使用websocket

function MySocket(path){
    this.path=path
    this.init=function() {


        if (typeof(WebSocket) === "undefined") {
            alert("您的浏览器不支持socket")
        } else {
            // 实例化socket
            this.socket = new WebSocket(this.path)
            // 监听socket连接
            this.socket.onopen = this.open
            // 监听socket错误信息
            this.socket.onerror = this.error
            // 监听socket消息
            this.socket.onmessage = this.getMessage
            this.socket.onclose = this.close
        }
        // console.log(this.socket)
    }


    this.open=function() {
        console.log("socket连接成功")



    }
    this.error=function() {

        console.log("连接错误,正在重连")

        // this.init()


        // this.socket = new WebSocket(this.path)
    }
    this.getMessage=function(msg) {
        console.log(msg)
    }
    this.send=function(params) {

    }
    this.close=function(evt) {
        console.log(evt.code)
        console.log(this.socket)
    }
}

 使用:

var socket1=new MySocket(‘path‘)
socket1.init()

 

网页使用websocket

上一篇:微信WeUI入门


下一篇:element upload 上传多文件时on-success方法只执行一次的解决