我是 Node.js 的新手,我正在使用 Socket.IO 开发一个简单的聊天应用程序。 我制作了一个 index.js 文件和一个 index.html 文件。
我的index.js 文件是这样的
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
process.stdin.resume();
process.stdin.setEncoding('utf8');
var util = require('util');
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
console.log('message: ' + msg);
});
var txt = process.stdin.on('data', function (text) {
process.tick()
return util.inspect(text)
});
socket.emit('chat message',txt);
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
我的 index.html 文件是这样的
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
</script>
</body>
</html>
我正在尝试从控制台获取值并将其打印在浏览器上,反之亦然。在此应用程序中,聊天发生在客户端和服务器之间。
当我运行 index.js 文件时,我在终端上得到了这个
listening on *:3000
一切都很好,直到我从浏览器中使用 localhost:3000 访问服务器,然后我在终端上得到了这个
buffer.js:0
(function (exports, require, module, __filename, __dirname) { // Copyright Joy
^
RangeError: Maximum call stack size exceeded
我该如何解决这个问题?我想从控制台(服务器)获取值并将其发送到浏览器(客户端)。
更新 1
我收到这个错误
process.tick()
^
TypeError: Object #<process> has no method 'tick'
at ReadStream.<anonymous> (/home/sunil/Downloads/TPL/chat/index.js:16:14)
at ReadStream.EventEmitter.emit (events.js:95:17)
at ReadStream.<anonymous> (_stream_readable.js:746:14)
at ReadStream.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at ReadStream.Readable.push (_stream_readable.js:127:10)
at TTY.onread (net.js:526:21)
更新 2 process.tick() 被替换为 process.nextTick() 然后我得到这个错误
node.js:415
callback();
^
TypeError: undefined is not a function
at process._tickCallback (node.js:415:13)
最佳答案
RangeError: Maximum call stack size exceeded. The error is generated by infinite loop somewhere in your app. Updated code inside io.on('connection' ...
var txt = '';
process.stdin.on('data', function (text) {
process.nextTick();
txt = util.inspect(text);
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
console.log('message: ' + msg);
socket.emit('chat message',txt);
});
});
Putting socket.emit inside io.on('connection', function(socket){}); directly will be continuously emitting that socket event' like a infinite loop and raises stack size range error. So, Please update your code as above.
关于javascript - 在nodejs中获取最大调用堆栈大小超出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37805366/