新建bash脚本counter.sh
#!/bin/bash # Count from 1 to 10 with a sleep for ((COUNT = 1; COUNT <= 10; COUNT++)); do echo $COUNT sleep 0.5 done
动服务
$ ./websocketd --port=8080 bash ./counter.sh • 1
客户端代码 demo.html
<script> var ws = new WebSocket("ws://localhost:8080/"); ws.onmessage = function (event) { console.log(event.data); }; </script>
参考