socket.on() 中的 console.log 没有输出
no output from console.log inside socket.on()
对于一项学校作业,我必须修改在 cloud9.io 上创建新 Node.js 工作区时打开的示例聊天应用程序。当我查看代码试图弄清楚发生了什么以及我需要更改什么时,我尝试输入一些 console.logs 来查看什么时候发生了什么,但由于某种原因大多数 console.logs 是当我 运行 应用程序时,从未在控制台中打印过。所以我试图理解这段代码并需要一些帮助!
未打印 console.log 的示例:
来自 index.html
<script>
function ChatController($scope) {
var socket = io.connect();
// ...
// some more $scope.on function
$scope.send = function send() {
console.log('Sending message:', $scope.text); // this one gets printed
socket.emit('message', $scope.text);
$scope.text = '';
};
}
</script>
来自 server.js
io.on('connection', function (socket) {
messages.forEach(function (data) {
socket.emit('message', data);
});
// this is the code that i don't get. I thought if I send a message,
// the socket.emit('message', $scope.text) would call this
// socket.on('message' ...) to send the msg, but the text in
// console.log is never printed in the console
socket.on('message', function (msg) {
console.log("sending a message?"); // does not get printed
var text = String(msg || '');
if (!text)
return;
socket.get('name', function (err, name) {
var data = {
name: name,
text: text
};
broadcast('message', data);
messages.push(data);
});
});
});
所以,是的,如果你们能指导我一点点解决这个问题,那就太好了!
谢谢!
console.log
会去两个不同的地方。对于您的 index.html
文件,他们将转到浏览器控制台,对于您的 server.js
文件,他们将转到您所在的终端 运行 Cloud9 中的应用程序。检查您要查找的特定日志的这些位置,您应该可以找到它们!
对于一项学校作业,我必须修改在 cloud9.io 上创建新 Node.js 工作区时打开的示例聊天应用程序。当我查看代码试图弄清楚发生了什么以及我需要更改什么时,我尝试输入一些 console.logs 来查看什么时候发生了什么,但由于某种原因大多数 console.logs 是当我 运行 应用程序时,从未在控制台中打印过。所以我试图理解这段代码并需要一些帮助!
未打印 console.log 的示例: 来自 index.html
<script>
function ChatController($scope) {
var socket = io.connect();
// ...
// some more $scope.on function
$scope.send = function send() {
console.log('Sending message:', $scope.text); // this one gets printed
socket.emit('message', $scope.text);
$scope.text = '';
};
}
</script>
来自 server.js
io.on('connection', function (socket) {
messages.forEach(function (data) {
socket.emit('message', data);
});
// this is the code that i don't get. I thought if I send a message,
// the socket.emit('message', $scope.text) would call this
// socket.on('message' ...) to send the msg, but the text in
// console.log is never printed in the console
socket.on('message', function (msg) {
console.log("sending a message?"); // does not get printed
var text = String(msg || '');
if (!text)
return;
socket.get('name', function (err, name) {
var data = {
name: name,
text: text
};
broadcast('message', data);
messages.push(data);
});
});
});
所以,是的,如果你们能指导我一点点解决这个问题,那就太好了! 谢谢!
console.log
会去两个不同的地方。对于您的 index.html
文件,他们将转到浏览器控制台,对于您的 server.js
文件,他们将转到您所在的终端 运行 Cloud9 中的应用程序。检查您要查找的特定日志的这些位置,您应该可以找到它们!