WebSocket(整理存档)

WebSocket的使用方法和api

 // 创建WebSocket连接
        var ws = new WebSocket('ws://localhost:8080'); // 这里的地址根据实际情况修改
        // 当WebSocket连接成功时
        ws.onopen = function () {
            console.log('WebSocket连接成功');
        };
        // 当接收到WebSocket消息时
        ws.onmessage = function (event) {
            var data = JSON.parse(event.data);
            // 将新数据添加到x轴和y轴数据数组中
             console.log(data)
            // 后面用数据更新图表
        };
        // 当WebSocket连接关闭时
        ws.onclose = function () {
            console.log('WebSocket连接关闭');
        };

WebSocket API 的属性和方法

  • binaryType:使用二进制的数据类型连接。
  • bufferedAmount:未发送至服务器的字节数。
  • extensions:服务器选择的扩展。
  • onclose:用于指定连接关闭后的回调函数。
  • onerror:用于指定连接失败后的回调函数。
  • onmessage:用于指定当从服务器接受到信息时的回调函数。
  • onopen:用于指定连接成功后的回调函数。
  • protocol:服务器选择的下属协议。
  • readyState:当前的链接状态。
  • url:WebSocket 的绝对路径。

WebSocket API 的事件

  • open:当一个 WebSocket 连接成功时触发。
  • message:当通过 WebSocket 收到数据时触发。
  • close:当一个 WebSocket 连接被关闭时触发。
  • error:当一个 WebSocket 连接因错误而关闭时触发。

WebSocket API 的应用场景

WebSocket 协议非常适用于基于 Web 的游戏、聊天应用,以及任何需要低延迟、实时连接的应用程序。例如:

  • 实时通信应用,如在线聊天、在线会议等场景。
  • 实时数据推送应用,包括股票交易平台、体育赛事实时比分推送等。
  • 在线游戏,对于多人在线游戏,玩家的操作和游戏状态的更新需要实时双向传递。

以上信息基于最新的搜索结果,希望能帮助您更好地理解 WebSocket API 的使用和应用场景。

HTML 5 Web Sockets is a powerful and effective technique for real-time information processing. There exists many techniques such as Poling, Long Poling and Streaming that are said to be better earier days. With web sockets, it shows a better outcome for the end user as well as a proper utilization of the server bandwidth. WebSocket is a web technology providing full-duplex communications channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API for in Web IDL is being standardized by the W3C.WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application. The WebSocket Protocol is an independent TCP-based protocol. Its only relationship to HTTP is that its handshake is interpreted by HTTP servers as an Upgrade request. The WebSocket protocol makes possible more interaction between a browser and a web site, facilitating live content and the creation of real-time games. This is made possible by providing a standardized way for the server to send content to the browser without being solicited by the client, and allowing for messages to be passed back and forth while keeping the connection open. In this way a two-way (bi-directional) ongoing conversation can take place between a browser and the server. A similar effect has been achieved in non-standardized ways using stop-gap technologies such as Comet.In addition, the communications are done over TCP port number 80, which is of benefit for those environments which block non-standard Internet connections using a firewall. WebSocket protocol is currently supported in several browsers including Google Chrome, Internet Explorer, Firefox, Safari and Opera. WebSocket also requires web applications on the server to support it. Here goes a comparison of polling vs Web Sockets.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值