SlideShare a Scribd company logo
HTML5 开发
Web,MobileWeb & Apps
范圣刚
安博中程在线
Web Sockets
持久连接和双向通信
Web Sockets 介绍
安博中程在线
目标:在一个持久连接上提供全双工的双向通信。
最开始是以TCPConnection的身份添加到HTML5 规范的, 后来被抽离出来形成了自
己的单独的规范。
浏览器支持:Firefox 6+, Safari 5+, Chrome和iOS 4以上的Safari。
·
·
·
3/18
Web Sockets 协议
Web Socket 使用了自定义的协议,而不是HTTP,所以URL模式也略有不同:
使用自定义协议而不是HTTP协议的好处是,能够在客户端和服务器之间发送非常少量
的数据,因此特别适合移动应用。
安博中程在线
未加密的连接是ws://
加密连接是wss://
·
·
4/18
Web Sockets API
建立连接
要创建Web Socket,先实例一个WebSocket 对象并传入要连接的URL:
注意:必须给WebSocket 构造函数传入绝对URL路径。
安博中程在线
varsocket=newWebSocket("ws://www.example.com/server.php"); JAVASCRIPT
5/18
Web Sockets API
关闭连接
要关闭Web Socket 连接,可以在任何时候调用close()方法。
安博中程在线
socket.close(); JAVASCRIPT
6/18
连接的建立过程
安博中程在线
实例化Web Socket 后,浏览器就会马上尝试创建HTTP连接;
浏览器发起连接,并取得服务器响应后,建立的连接会从使用HTTP协议升级为
Web Socket协议。
·
·
7/18
连接状态
WebSocket 有一个表示当前状态的readyState 属性:
readyState 的值永远从0开始。
安博中程在线
WebSocket.OPENING(0): 正在建立连接
WebSocket.OPEN(1): 已经建立连接
WebSocket.CLOSING(2): 正在关闭连接
WebSocket.CLOSE(3): 已经关闭连接
·
·
·
·
8/18
发送简单数据
Web Socket 打开之后,就可以通过连接发送和接收数据。
发送数据使用send()方法,并传入任意字符串。例如:
安博中程在线
varsocket=newWebSocket("ws://www.example.com/foobar.php");
socket.send("Helloworld!");
JAVASCRIPT
9/18
发送复杂数据
下面的代码是把数据序列化成JSON字符串,再发送到服务器:
服务器收到以后,再解析收到的JSON字符串。
安博中程在线
Web Sockets只能通过连接发送纯文本数据;
对于复杂的数据结构,发送前要进行序列化;
·
·
varmessage={
time:newDate(),
text:"HelloWorld!",
clientId:"adkeig393g8dk"
};
socket.send(JSON.stringify(message));
JAVASCRIPT
10/18
接收数据
当服务器向客户端发来消息时,Web Socket 对象会触发message事件。这个
message事件同样把返回的数据保存在event.data中。
与通过send()发送到服务器的数据一样,event.data中返回的数据也是字符串。大部
分情况下也需要手工解析。
安博中程在线
socket.onmessage=function(event){
vardata=event.data;
//处理数据
};
JAVASCRIPT
11/18
其他事件
在连接生命周期不同阶段触发的其他三个事件(除了message):
安博中程在线
open: 在成功建立连接时触发
error: 发生错误时触发,连接不能持续
close: 在连接关闭时触发
·
·
·
12/18
代码:其他事件
安博中程在线
varsocket=newWebSocket("ws://www.example.com/server.php");
socket.onopen=function(){
alert("连接建立!");
}
socket.onerror=function(){
alert("连接出错!");
}
socket.onclose=function(event){
console.log("连接关闭!"+"Wasclean?"+event.wasClean+"Code="+event.code+"Reason="
}
JAVASCRIPT
13/18
close事件的属性
前面的三个事件中只有close事件的event对象有额外的三个属性信息:
安博中程在线
wasClean是一个布尔值,表示连接是否已经明确的关闭;
code 是服务器返回的数值状态码;
reason是一个字符串,包含服务器发回的信息。
·
·
·
socket.onclose=function(event){
console.log("连接关闭!"+"Wasclean?"+event.wasClean+
"Code="+event.code+"Reason="+event.reason);
}
JAVASCRIPT
14/18
Demo
即时通信聊天室
使用Web Socket 进行即时通信
昵称 访客 更改
正在连接....
连接关闭
消息 发送
安博中程在线 16/18
扩展阅读
安博中程在线
Pusher·
17/18
<Thank you!>
Feel free to contact me if you have any question.
微博 @范圣刚
博客 www.tfan.org
github github.com/princetoad

More Related Content

Viewers also liked

Html5 最重要的部分
Html5 最重要的部分Html5 最重要的部分
Html5 最重要的部分Tom Fan
 
Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference Counting
Giuseppe Arici
 
Where'd all my memory go? SCALE 12x SCALE12x
Where'd all my memory go? SCALE 12x SCALE12xWhere'd all my memory go? SCALE 12x SCALE12x
Where'd all my memory go? SCALE 12x SCALE12x
Joshua Miller
 
Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)
Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)
Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)James Clause
 
Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Tom Fan
 
Html5 history
Html5 historyHtml5 history
Html5 historyTom Fan
 

Viewers also liked (6)

Html5 最重要的部分
Html5 最重要的部分Html5 最重要的部分
Html5 最重要的部分
 
Automatic Reference Counting
Automatic Reference CountingAutomatic Reference Counting
Automatic Reference Counting
 
Where'd all my memory go? SCALE 12x SCALE12x
Where'd all my memory go? SCALE 12x SCALE12xWhere'd all my memory go? SCALE 12x SCALE12x
Where'd all my memory go? SCALE 12x SCALE12x
 
Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)
Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)
Advanced Dynamic Analysis for Leak Detection (Apple Internship 2008)
 
Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发Android 平台 HTML5 应用开发
Android 平台 HTML5 应用开发
 
Html5 history
Html5 historyHtml5 history
Html5 history
 

Similar to Web sockets

[圣思园][Java SE]Network
[圣思园][Java SE]Network[圣思园][Java SE]Network
[圣思园][Java SE]NetworkArBing Xie
 
Html5
Html5Html5
Html5
cazhfe
 
Web开发基础
Web开发基础Web开发基础
Web开发基础
dynaturtle
 
组网实践
组网实践组网实践
组网实践
telab
 
Hongxing
HongxingHongxing
Hongxing
ncmooc
 
2018 Week 11 Bluetooth and Wi-Fi
2018 Week 11 Bluetooth and Wi-Fi2018 Week 11 Bluetooth and Wi-Fi
2018 Week 11 Bluetooth and Wi-Fi
AkashaC1
 
PHP & AppServ
PHP & AppServPHP & AppServ
PHP & AppServ
Ht Wang
 
Javascript加载总结
Javascript加载总结Javascript加载总结
Javascript加载总结
衡锋 阳
 
Re Introduce Web Development
Re Introduce Web DevelopmentRe Introduce Web Development
Re Introduce Web Developmentfinian lau
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型Tom Fan
 
ForumSentry客戶解決
ForumSentry客戶解決ForumSentry客戶解決
ForumSentry客戶解決Kevin Kao
 
LineBot
LineBotLineBot
LineBot
艾鍗科技
 
PHP WEB 应用组织与结构
PHP WEB 应用组织与结构PHP WEB 应用组织与结构
PHP WEB 应用组织与结构
HonestQiao
 
什麼是Nephio?
什麼是Nephio?什麼是Nephio?
什麼是Nephio?
秀吉(Hsiu-Chi) 蔡(Tsai)
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送yongboy
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送
yongboy
 
mobile_netis無線網路模式 - Client
mobile_netis無線網路模式 - Clientmobile_netis無線網路模式 - Client
mobile_netis無線網路模式 - Client臺灣塔米歐
 
mobile_netis無線網路模式 - Client
mobile_netis無線網路模式 - Clientmobile_netis無線網路模式 - Client
mobile_netis無線網路模式 - Client臺灣塔米歐
 
云桌面远程传输技术综述-李承东
云桌面远程传输技术综述-李承东云桌面远程传输技术综述-李承东
云桌面远程传输技术综述-李承东承东 李
 

Similar to Web sockets (20)

[圣思园][Java SE]Network
[圣思园][Java SE]Network[圣思园][Java SE]Network
[圣思园][Java SE]Network
 
Html5
Html5Html5
Html5
 
Web开发基础
Web开发基础Web开发基础
Web开发基础
 
组网实践
组网实践组网实践
组网实践
 
Hongxing
HongxingHongxing
Hongxing
 
2018 Week 11 Bluetooth and Wi-Fi
2018 Week 11 Bluetooth and Wi-Fi2018 Week 11 Bluetooth and Wi-Fi
2018 Week 11 Bluetooth and Wi-Fi
 
PHP & AppServ
PHP & AppServPHP & AppServ
PHP & AppServ
 
Javascript加载总结
Javascript加载总结Javascript加载总结
Javascript加载总结
 
Re Introduce Web Development
Re Introduce Web DevelopmentRe Introduce Web Development
Re Introduce Web Development
 
HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型HTML5 生态系统和应用架构模型
HTML5 生态系统和应用架构模型
 
ForumSentry客戶解決
ForumSentry客戶解決ForumSentry客戶解決
ForumSentry客戶解決
 
LineBot
LineBotLineBot
LineBot
 
PHP WEB 应用组织与结构
PHP WEB 应用组织与结构PHP WEB 应用组织与结构
PHP WEB 应用组织与结构
 
什麼是Nephio?
什麼是Nephio?什麼是Nephio?
什麼是Nephio?
 
06
0606
06
 
Real time web实时信息流推送
Real time web实时信息流推送Real time web实时信息流推送
Real time web实时信息流推送
 
Real-Time Web实时信息流推送
Real-Time Web实时信息流推送Real-Time Web实时信息流推送
Real-Time Web实时信息流推送
 
mobile_netis無線網路模式 - Client
mobile_netis無線網路模式 - Clientmobile_netis無線網路模式 - Client
mobile_netis無線網路模式 - Client
 
mobile_netis無線網路模式 - Client
mobile_netis無線網路模式 - Clientmobile_netis無線網路模式 - Client
mobile_netis無線網路模式 - Client
 
云桌面远程传输技术综述-李承东
云桌面远程传输技术综述-李承东云桌面远程传输技术综述-李承东
云桌面远程传输技术综述-李承东
 

More from Tom Fan

Semantics
SemanticsSemantics
SemanticsTom Fan
 
Multimedia
MultimediaMultimedia
MultimediaTom Fan
 
Intro to-html5
Intro to-html5Intro to-html5
Intro to-html5Tom Fan
 
Geolocation
GeolocationGeolocation
GeolocationTom Fan
 
File api
File apiFile api
File apiTom Fan
 
Webstorage
WebstorageWebstorage
WebstorageTom Fan
 
AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状Tom Fan
 
18 NSUserDefaults
18 NSUserDefaults18 NSUserDefaults
18 NSUserDefaults
Tom Fan
 
17 Localization
17 Localization17 Localization
17 Localization
Tom Fan
 
16 CoreData
16 CoreData16 CoreData
16 CoreData
Tom Fan
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCell
Tom Fan
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application States
Tom Fan
 
13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller
Tom Fan
 
12 Camera
12 Camera12 Camera
12 Camera
Tom Fan
 
11 UINavigationController
11 UINavigationController11 UINavigationController
11 UINavigationController
Tom Fan
 
10 Editing UITableView
10 Editing UITableView10 Editing UITableView
10 Editing UITableView
Tom Fan
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewControllerTom Fan
 
08 Notification and Rotation
08 Notification and Rotation08 Notification and Rotation
08 Notification and Rotation
Tom Fan
 
07 View Controllers
07 View Controllers07 View Controllers
07 View Controllers
Tom Fan
 

More from Tom Fan (20)

Semantics
SemanticsSemantics
Semantics
 
Multimedia
MultimediaMultimedia
Multimedia
 
Intro to-html5
Intro to-html5Intro to-html5
Intro to-html5
 
Geolocation
GeolocationGeolocation
Geolocation
 
File api
File apiFile api
File api
 
Css3
Css3Css3
Css3
 
Webstorage
WebstorageWebstorage
Webstorage
 
AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状AT&T 的 HTML5 策略和应用现状
AT&T 的 HTML5 策略和应用现状
 
18 NSUserDefaults
18 NSUserDefaults18 NSUserDefaults
18 NSUserDefaults
 
17 Localization
17 Localization17 Localization
17 Localization
 
16 CoreData
16 CoreData16 CoreData
16 CoreData
 
15 Subclassing UITableViewCell
15 Subclassing UITableViewCell15 Subclassing UITableViewCell
15 Subclassing UITableViewCell
 
14 Saving Loading and Application States
14 Saving Loading and Application States14 Saving Loading and Application States
14 Saving Loading and Application States
 
13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller13 UIPopoverController and Modal View Controller
13 UIPopoverController and Modal View Controller
 
12 Camera
12 Camera12 Camera
12 Camera
 
11 UINavigationController
11 UINavigationController11 UINavigationController
11 UINavigationController
 
10 Editing UITableView
10 Editing UITableView10 Editing UITableView
10 Editing UITableView
 
09 UITableView and UITableViewController
09 UITableView and UITableViewController09 UITableView and UITableViewController
09 UITableView and UITableViewController
 
08 Notification and Rotation
08 Notification and Rotation08 Notification and Rotation
08 Notification and Rotation
 
07 View Controllers
07 View Controllers07 View Controllers
07 View Controllers
 

Web sockets