node-websocket-server TypeError:参数必须是字符串
我在 ubuntu 上跟踪 /var/log/logfile,并收到 TypeError。适用于我的本地 Mac 计算机 /var/log/system.log。
命令:
sudo node server.js /var/log/logfile(必须是 sudo 才能查看此日志文件
)
错误:
/npm/node_modules/node-websocket-server/lib/ws/connection.js:290
var byteLen = Buffer.byteLength(data, 'utf8'),
^
TypeError: Argument must be a string
at Connection.<anonymous> (/npm/node_modules/node-websocket-server/lib/ws/connection.js:290:26)
at clientWrite (//npm/node_modules/node-websocket-server/lib/ws/server.js:34:12)
at /npm/node_modules/node-websocket-server/lib/ws/server.js:99:7
at Manager.forEach (/npm/node_modules/node-websocket-server/lib/ws/manager.js:125:14)
at Server.broadcast (npm/node_modules/node-websocket-server/lib/ws/server.js:98:13)
at Socket.<anonymous> (/npm/server.js:63:11)
at Socket.emit (events.js:64:17)
at Socket._onReadable (net.js:677:14)
at IOWatcher.onReadable [as callback] (net.js:177:10)
代码:
/*
* server.js
*/
var util = require('util');
var sys = require("sys");
var ws = require("/npm/node_modules/node-websocket-server");
var spawn = require('child_process').spawn;
var filename = process.ARGV[2];
if (!filename)
return sys.puts("Usage: node <server.js> <filename>");
var server = ws.createServer({debug: true});
/*
* After server comes up
*/
server.addListener("listening", function() {
sys.log("Listening for connections on localhost:9997");
});
/*
* Define any port
*/
server.listen(9997);
var tail = spawn("tail", ["-f", filename]);
sys.puts("start tailing");
tail.stdout.on("data", function (data) {
/*
* Send data
*/
server.broadcast(data);
console.log('' + data);
});
I'm tailing a /var/log/logfile on ubuntu, and getting a TypeError. Works on my local Mac machine /var/log/system.log.
Command:
sudo node server.js /var/log/logfile (must be sudo to view this log file
)
Error:
/npm/node_modules/node-websocket-server/lib/ws/connection.js:290
var byteLen = Buffer.byteLength(data, 'utf8'),
^
TypeError: Argument must be a string
at Connection.<anonymous> (/npm/node_modules/node-websocket-server/lib/ws/connection.js:290:26)
at clientWrite (//npm/node_modules/node-websocket-server/lib/ws/server.js:34:12)
at /npm/node_modules/node-websocket-server/lib/ws/server.js:99:7
at Manager.forEach (/npm/node_modules/node-websocket-server/lib/ws/manager.js:125:14)
at Server.broadcast (npm/node_modules/node-websocket-server/lib/ws/server.js:98:13)
at Socket.<anonymous> (/npm/server.js:63:11)
at Socket.emit (events.js:64:17)
at Socket._onReadable (net.js:677:14)
at IOWatcher.onReadable [as callback] (net.js:177:10)
Code:
/*
* server.js
*/
var util = require('util');
var sys = require("sys");
var ws = require("/npm/node_modules/node-websocket-server");
var spawn = require('child_process').spawn;
var filename = process.ARGV[2];
if (!filename)
return sys.puts("Usage: node <server.js> <filename>");
var server = ws.createServer({debug: true});
/*
* After server comes up
*/
server.addListener("listening", function() {
sys.log("Listening for connections on localhost:9997");
});
/*
* Define any port
*/
server.listen(9997);
var tail = spawn("tail", ["-f", filename]);
sys.puts("start tailing");
tail.stdout.on("data", function (data) {
/*
* Send data
*/
server.broadcast(data);
console.log('' + data);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前node-websocket-server只接受Strings形式的数据,不接受Buffers形式的数据。缓冲区将来可能会被接受,这很大程度上是由于框架的限制以及我不想进行大量缓冲区复制的事实。 (仅向套接字写入 0xFF 或 0x00 似乎是不负责任的)。
另外,尝试这样做:
由于 stdout 和 stderr 的流默认不编码,这意味着 data 事件会发出 Buffer 对象。至于为什么这在你的 mac 上有效但在 ubuntu 上失败,我不确定,但刚才做了一个快速测试,如果我没有专门设置编码,我的 ubuntu 和 mac 都会给我缓冲区对象溪流。
At present node-websocket-server only accepts data in the form of Strings, not Buffers. Buffers may be accepted in the future, this is pretty much due to the limitation of framing and the fact that I don't want to be doing large amounts of buffer copying. (It seems irresponsible to write out just 0xFF or 0x00 to a socket).
Also, try doing:
As the streams of stdout and stderr default to no encoding, meaning that the data event emits Buffer objects. As to why this works on your mac but fails on ubuntu, I'm not sure, but doing a quick test just a moment ago, I have both ubuntu and mac giving me buffer objects if I didn't specifically set the encoding of the streams.