内容简介:传统的 http 协议通信只能由客户端发起,并且它是一个单向请求,这种单向请求的特点,注定了如果服务器有连续的状态变化,客户端要获知就非常麻烦。我们只能使用”轮询”:每隔一段时候,就发出一个询问,了解服务器有没有新的信息。最典型的场景就是聊天室。轮询的效率低,非常浪费资源(因为必须不停连接,或者 HTTP 连接始终打开)。因此,工程师们一直在思考,有没有更好的方法。WebSocket 就是这样发明的。WebSocket 协议在2008年诞生,2011年成为国际标准。所有浏览器都已经支持了。
传统的 http 协议通信只能由客户端发起,并且它是一个单向请求,这种单向请求的特点,注定了如果服务器有连续的状态变化,客户端要获知就非常麻烦。我们只能使用”轮询”:每隔一段时候,就发出一个询问,了解服务器有没有新的信息。最典型的场景就是聊天室。
轮询的效率低,非常浪费资源(因为必须不停连接,或者 HTTP 连接始终打开)。因此,工程师们一直在思考,有没有更好的方法。WebSocket 就是这样发明的。
WebSocket 协议在2008年诞生,2011年成为国际标准。所有浏览器都已经支持了。
它的最大特点就是,服务器可以主动向客户端推送信息,客户端也可以主动向服务器发送信息,是真正的双向平等对话,属于服务器推送技术的一种。
其他特点包括:
-
建立在 TCP 协议之上,服务器端的实现比较容易。
-
与 HTTP 协议有着良好的兼容性。默认端口也是80和443,并且握手阶段采用 HTTP 协议,因此握手时不容易屏蔽,能通过各种 HTTP 代理服务器。
-
数据格式比较轻量,性能开销小,通信高效。
-
可以发送文本,也可以发送二进制数据。
-
没有同源限制,客户端可以与任意服务器通信。
-
协议标识符是ws(如果加密,则为wss),服务器网址就是 URL。
如:
ws://example.com:80/some/path
websocket 客户端 API
websocketd
WebSocket 服务器 Websocketd 最大特点,就是后台脚本不限语言,标准输入(stdin)就是 WebSocket 的输入,标准输出(stdout)就是 WebSocket 的输出。
下载
点击这里 下载
解压
[root@centos ~]# ls counter.sh websocketd-0.3.0-linux_amd64.zip [root@centos ~]# unzip websocketd-0.3.0-linux_amd64
创建一个连接
1.先准备一个脚本
[root@centos ~]# cat counter.sh #!/bin/bash echo 1 sleep 1 echo 2 sleep 1 echo 3
2.设置监听端口并执行脚本
[root@centos ~]# ./websocketd --port=80 bash ./counter.sh
客户端
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript">
var ws = new WebSocket('ws://59.111.110.90:80/');
ws.onmessage = function(event) {
console.log(event.data);
};
</script>
</head>
<body>
</body>
</html>
刷新页面在console 中会打印
服务端
Fri, 19 Jan 2018 09:43:14 +0800 | INFO | server | | Serving using application : /usr/bin/bash ./counter.sh Fri, 19 Jan 2018 09:43:14 +0800 | INFO | server | | Starting WebSocket server : ws://centos.novalocal:80/ Fri, 19 Jan 2018 09:46:08 +0800 | ACCESS | session | url:'http://59.111.110.90/' id:'1516326368262661241' remote:'10.173.32.233' command:'/usr/bin/bash' origin:'file://' | CONNECT Fri, 19 Jan 2018 09:46:10 +0800 | ACCESS | session | url:'http://59.111.110.90/' id:'1516326368262661241' remote:'10.173.32.233' command:'/usr/bin/bash' origin:'file://' pid:'948' | DISCONNECT Fri, 19 Jan 2018 09:46:17 +0800 | ACCESS | session | url:'http://59.111.110.90/' id:'1516326377566650836' remote:'10.173.32.233' command:'/usr/bin/bash' origin:'file://' | CONNECT Fri, 19 Jan 2018 09:46:19 +0800 | ACCESS | session | url:'http://59.111.110.90/' id:'1516326377566650836' remote:'10.173.32.233' command:'/usr/bin/bash' origin:'file://' pid:'955' | DISCONNECT Fri, 19 Jan 2018 09:49:51 +0800 | ACCESS | session | url:'http://59.111.110.90/' id:'1516326591176543193' remote:'10.173.32.233' command:'/usr/bin/bash' origin:'file://' | CONNECT Fri, 19 Jan 2018 09:49:53 +0800 | ACCESS | session | url:'http://59.111.110.90/' id:'1516326591176543193' remote:'10.173.32.233' command:'/usr/bin/bash' origin:'file://' pid:'1002' | DISCONNECT Fri, 19 Jan 2018 09:49:56 +0800 | ACCESS | session | url:'http://59.111.110.90/' id:'1516326596164276971' remote:'10.173.32.233' command:'/usr/bin/bash' origin:'file://' | CONNECT Fri, 19 Jan 2018 09:49:58 +0800 | ACCESS | session | url:'http://59.111.110.90/' id:'1516326596164276971' remote:'10.173.32.233' command:'/usr/bin/bash' origin:'file://' pid:'1007' | DISCONNECT
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- golang分层测试之单元测试-testing使用
- golang分层测试之单元测试-gocheck使用
- 测试工程师如何使用 CODING 进行测试管理
- Django使用心得(二) 使用TestCase测试接口
- 使用“数据驱动测试”之前
- 使用 Gomock 进行单元测试
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Caching
Duane Wessels / O'Reilly Media, Inc. / 2001-6 / 39.95美元
On the World Wide Web, speed and efficiency are vital. Users have little patience for slow web pages, while network administrators want to make the most of their available bandwidth. A properly design......一起来看看 《Web Caching》 这本书的介绍吧!