简体   繁体   English

Node.JS服务器未发送数据(或客户端未接收)

[英]Node.JS server not sending data (or client not receiving)

I have a node.js server (using socket.io and express). 我有一个node.js服务器(使用socket.io和express)。 I had everything functioning, but went to make my code into multiple files for organization. 我一切正常,但是将我的代码分成多个文件进行组织。

Now, the client can send to the server (and the server can receive the data), but the server isn't sending the data back to the client ( or the client isn't receiving the sent data). 现在,客户端可以发送到服务器了(服务器可以接收数据),但是服务器没有将数据发送回客户端( 或者客户端没有接收到发送的数据)。

Here's my server (app.js): 这是我的服务器(app.js):

var variables = require('./variables');
var functions = require('./functions');
var connection = require('./connections');
var heartbeat = require('./heartbeat');

var mongoClient = require('mongodb').MongoClient;
var config = JSON.parse(process.env.APP_CONFIG);
var mongoPassword = '1029384756';

variables.app.use('/client', variables.express.static(__dirname + '/client'));

mongoClient.connect('mongodb://' + config.mongo.user + ":" + mongoPassword + "@" + config.mongo.hostString, function (err, db) {
    if (err) throw err;

    variables.db = db;

    // Routing
    variables.app.get('/', function(request, response) {
        response.sendFile(variables.path.join(__dirname, '/client/index.html'));
    });

    variables.server.listen(process.env.PORT);

    heartbeat.start();

    connection.startListening();
});

My variables.js: 我的variables.js:

var http = require('http');
var express = require('express');
var socketIO = require('socket.io');
var path = require('path');
var app = express();
var server = http.Server(app);
var io = socketIO(server);
var db = null;
var onlinePlayers = {};


module.exports = {
    http,
    express,
    socketIO,
    path,
    app,
    server,
    io,
    db,
    onlinePlayers
}

My connections.js (where my io.on is) 我的connections.js(我的io.on在哪里)

const variables = require('./variables');

module.exports = {
    startListening: function () {
    variables.io.on('connection', function(socket) {
        socket.emit('message', "Testing sending from server.");

        socket.on('message', function(message) {
            console.log(message);
            socket.emit('message', message);
        });

    --Snipped more variables.io.on (ignore {} mistakes, syntax is correct in code)
}

And my client (again, extremely snipped) 和我的客户(再次,极其可笑)

var socket = io();
var page = 0;

console.log("Loaded");

window.onload = function() {
    setPage(0);
}

socket.on('message', function(message) {
    console.log(message);
});

What will happen is, this: 将会发生的是:

Page will load. 页面将加载。 Client will print "Loaded", and then will print "Testing sending from server." 客户端将打印“已加载”,然后打印“正在测试从服务器发送”。 (because of the socket.emit upon connection in connection.js) (由于socket.emit在connection.js中连接时发出)

If I go into the Client/Webpage's console and type: 如果进入客户端/网页的控制台并键入:

socket.emit('message', "Hello");

My server will print out "Hello" (as it should), however my client doesn't then get the socket.emit from the server to print "Hello" back. 我的服务器将打印出“ Hello”(应该如此),但是我的客户端随后没有从服务器获取socket.emit来打印“ Hello”。

I'm completely stumped. 我完全迷住了。 Any help? 有什么帮助吗?

I figured it out. 我想到了。 At some point in my code (in this case it was inside of the setPage(0) function), the tag for the index.js file that had the socket.ons was getting removed. 在我的代码中的某个时刻(在这种情况下,它位于setPage(0)函数内部),具有socket.ons的index.js文件的标记已被删除。 Which means the server was sending the data, and the client was getting it, but just didn't have the script to tell it what to do with that data. 这意味着服务器正在发送数据,而客户端正在获取数据,但是没有脚本告诉它如何处理该数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM