websocketd 是WebSocket守护进程,它负责处理WebSocket连接,启动您的程序来处理WebSockets,并在程序和Web浏览器之间传递消息。
安装:websocketd
wget https://github.com/joewalnes/websocketd/releases/download/v0.2.12/websocketd-0.2.12-linux_amd64.zip
unzip websocketd-0.2.12-linux_amd64.zip
解压后生成这个文件:websocketd
复制该文件到 /usr/bin目录下,修改环境变量
sudo cp websocketd /usr/bin/websocketd sudo vim /etc/profile export PATH=$PATH:/usr/bin/websocketd
可能出现的错误:如果修改了/etc/profile,那么编辑结束后执行source profile 或 执行点命令 ./profile,PATH的值就会立即生效了,但是会提示以下错误:
#source /etc/profile 之后为什么会出现
command not found
解决办法,直接切换到root 用户模式既可,再次执行source profile 就可以了,输入help 看是否配置合适,如下所示:
tinywan@tinywan:~/shell$ websocketd --help websocketd (0.2.12 (go1.6 linux-amd64) --) websocketd is a command line tool that will allow any executable program that accepts input on stdin and produces output on stdout to be turned into a WebSocket server. Usage: Export a single executable program a WebSocket server: websocketd [options] COMMAND [command args] Or, export an entire directory of executables as WebSocket endpoints: websocketd [options] --dir=SOMEDIR Options: --port=PORT HTTP port to listen on. --address=ADDRESS Address to bind to (multiple options allowed) Use square brackets to specify IPv6 address. Default: "" (all)
开启WebSocketd 服务Tinywan
tinywan@tinywan:~/shell$ websocketd --port=63800 ./count.sh Mon, 08 May 2017 17:26:50 +0800 | INFO | server | | Serving using application : ./count.sh Mon, 08 May 2017 17:26:50 +0800 | INFO | server | | Starting WebSocket server : ws://tinywan:8080/
测试代码:count.sh
#!/bin/bash for ((COUNT = 1; COUNT <= 10; COUNT++)); do
echo $COUNT sleep 1
done
运行脚本时你可能会遇到以下错误:
root@TinywanAliYun:/home/www/bin# websocketd --port=63800 ./count.sh Unable to locate specified COMMAND './count.sh' in OS path. Usage: Export a single executable program a WebSocket server: websocketd [options] COMMAND [command args] Or, export an entire directory of executables as WebSocket endpoints: websocketd [options] --dir=SOMEDIR Or, show extended help message using: websocketd --help
请赋予权限,使其可执行:
$ chmod +x ./count.sh
随便打开一个浏览器,在console中输入一下代码测试:
var ws = new WebSocket('ws://192.168.18.12:63800/'); ws.onopen = function() { console.log('CONNECT'); }; ws.onclose = function() { console.log('DISCONNECT'); }; ws.onmessage = function(event) { console.log('MESSAGE: ' + event.data); };
(1)测试结果如下所示:
(2)新建立一个客户端测试client.html
<!doctype html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>websocketd</title>
</head>
<body>
<h2>websocketd 客户端的简单测试</h2>
<pre id="log"></pre>
<script>
// helper function: log message to screen
function log(msg) { document.getElementById('log').textContent += msg + '\n'; } // setup websocket with callbacks
var ws = new WebSocket('ws://192.168.18.12:8080/'); ws.onopen = function() { console.log('CONNECT'); }; ws.onclose = function() { console.log('DISCONNECT'); }; ws.onmessage = function(event) { console.log('MESSAGE: ' + event