基础
工具
服务器
下载http://websocketd.com/
添加脚本count.sh
然后添加权限chmod +x count.sh
1 2 3 4 5
| #!/bin/bash for ((COUNT = 1; COUNT <= 10; COUNT++)); do echo $COUNT sleep 1 done
|
启动 ./websocketd --port=8080 ./count.sh
服务端
客户端
https://jsbin.com/zemigup/edit?js,console在该网页运行下面的脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| var ws = new WebSocket("ws://127.0.0.1:8080");
ws.onopen = function(evt) { console.log("Connection open ..."); ws.send("Hello WebSockets!"); };
ws.onmessage = function(evt) { console.log( "Received Message: " + evt.data); ws.close(); };
ws.onclose = function(evt) { console.log("Connection closed."); };
|
上面不支持非127.0.0.1的,测试ws://10.30.6.10:8080需要在http://www.blue-zero.com/WebSocket/测试
参考
官网
WebSocket 教程-阮一峰