- 7
- 0
- 约8.13千字
- 约 13页
- 2017-07-25 发布于河南
- 举报
html5的websocket基本资料(Websocket basic information about HTML5)
html5的websocket基本资料(Websocket basic information about HTML5)
转自http: / / blogs.c? om / wei2yi / archive / 20? 11 / 03 / 23 / 1992830.htm? the
认识html5的websocket
在html5规范中, 我最喜欢的web技术就是正迅速变得流行的websocket api.websocket提供了一个受欢迎的技术, 以替代我们过去几年一直在用的ajax技术.这个新的api提供了一个方法, 从客户端使用简单的语法有效地推动消息到服务器.让我们看一看html5的websocket bees: 它可用于客户端、服务器端.而且有一个优秀的第三方api, 名为 socket.io.
一、什么是websocket bees?
websocket api是下一代客户端 - 服务器的异步通信方法.该通信取代了单个的tcp套接字, 使用ws或wss协议, 可用于任意的客户端和服务器程序.websocket目前由w3c进行标准化.websocket已经受到firefox 4、chrome 4、opera 10.70以及safari 5等浏览器的支持.
websocket api最伟大之处在于服务器和客户端可以在给定的时间范围内的任意时刻, 相互推送信息.websocket并不限于以ajax (或xhr) 方式通信, 因为ajax技术需要客户端发起请求, 而websocket服务器和客户端可以彼此相互推送信息; xhr受到域的限制, 而websocket允许跨域通信.
ajax技术很聪明的一点是没有设计要使用的方式.websocket为指定目标创建, 用于双向推送消息.
二、websocket api的用法
只专注于客户端的api, 因为每个服务器端语言有自己的api.下面的代码片段是打开一个连接, 为连接创建事件监听器, 断开连接, 消息时间, 发送消息返回到服务器, 关闭连接.
[copy to clipboard] [-]
code:
/ / 创建一个socket实例
var = new socket websocket (nb: / / localhost: 8080).
/ / 打开socket
socket.onopen = function (event) {
/ / 发送一个初始化消息
socket.send (i am the client and the \s m listening!) ;
/ / 监听消息
socket.onmessage = function (event) {
console.log (client received a message, event);
};
/ / 监听socket的关闭
socket.onclose = function (event) {
console.log (notified client socket has closed, event);
};
/ / 关闭socket
/ / socket.close ()
};
让我们来看看上面的初始化片段.参数为url, ws表示websocket协议.onopen、onclose和onmessage方法把事件连接到socket实例上.每个方法都提供了一个事件, 以表示socket的状态.
onmessage事件提供了一个data属性, 它可以包含消息的body部分.
The Body part of the message must be a string that can be serialized / de serialized to pass more data.
The syntax of WebSocket is very simple and its incredibly easy to use WebSockets Unless the client does not support WebSocket. IE browsers do not currently support WebSocket communications. If your client does not support WebSocket communication, there are several backup scenarios for you to use:
Flash technology - Flash can provide a simple replacement. The most obvious drawback of using Flash is that not all clients
您可能关注的文档
- 英语写作高分短语(English writing high score phrases).doc
- 英语单词起源(Origin of English words).doc
- 英语发展史(History of English Development).doc
- 英语口语大赛策划书(Oral English contest book).doc
- 英语同意词(English agreed words).doc
- 英语名著(English classics).doc
- 英语听力方法(English listening methods).doc
- 英语发音十大规律(Ten major rules of English pronunciation).doc
- 英语听力 文化大国(English listening culture is a great country).doc
- 英语四六级作文写作句型(English composition at 46 level).doc
- html中级教程 不良标签(HTML intermediate tutorial bad label).doc
- html代码讲解(HTML code to explain).doc
- html常用代码(HTML common code).doc
- html手写代码(HTML handwritten code).doc
- html特殊字符编码(HTML special character encoding).doc
- huikuhjkhbkj(huikuhjkhbkj).doc
- hyrtu(hyrtu).doc
- h股与a股溢价对比(Compared with a premium H).doc
- ie部如何协助车间提高生产效率.及影响车间效率低下的原因docx(How does the IE department assist the workshop in improving production efficiency and affecting the efficiency of the workshop docx).doc
- iges破面修补(iges破面修补).doc
原创力文档

文档评论(0)