首页 > Web开发 > 详细

网页使用websocket

时间:2021-04-02 17:30:53      阅读:24      评论:0      收藏:0      [点我收藏+]
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

原文:https://www.cnblogs.com/azure-zero/p/14610867.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!