简体   繁体   English

Node.js跨端口发送数据包

[英]Node.js Emit Packet across Ports

I have a server.js and client.js pair of script files. 我有一对server.js和client.js脚本文件。

Here is the server.js file... 这是server.js文件...

var express = require('express');
var appMain = express(); var appReset = express();
var serverMain = require('http').Server(appMain);
var serverReset = require('http').Server(appReset);
var fs = require('fs');

appMain.get('/', function(req, res) {
    res.sendFile(__dirname + '/client/index.html');
});
appMain.use('/client', express.static(__dirname + '/client'));

appReset.get('/', function(req, res) {
    res.sendFile(__dirname + '/client/reset.html');
});
appReset.use('/client', express.static(__dirname + '/client'));

serverMain.listen(3000); console.log('Start mainServer');
serverReset.listen(2000); console.log('Start resetServer');

var gameScores;

fs.readFile('client/data/scores.txt', 'utf8', function(error, data) {
    if (error) {
        console.log('Read Error!');
        gameScores = null;
    }
    else {
        console.log('Read Success!');
        gameScores = data;
    }
});

var ioMain = require('socket.io') (serverMain,{});
ioMain.sockets.on('connection', function(socket) {
    console.log('mainSocket Connected');
    socket.emit('gameScores', gameScores);
    socket.on('gameScores', function(data) {
        gameScores = data;
        fs.writeFile('client/data/scores.txt', JSON.stringify(data), function(error) {
            if (error)
                console.log('Write Error!');
            else
                console.log('Write Success!');
        });
        socket.emit('gameScores', gameScores);
    });
});

var ioReset = require('socket.io') (serverReset,{});
ioReset.sockets.on('connection', function(socket) {
    console.log('resetSocket Connected');
    socket.on('resetScores', function(data) {
        fs.unlink('client/data/scores.txt', function(error) {
            if (error) {
                console.log('Delete Error!');
                socket.emit('clearScores', false);
            }
            else {
                console.log('Delete Success!');
                gameScores = null;
                socket.emit('defaultScores', gameScores);
                socket.emit('clearScores', true);
            }
        });
    });
});

Here is the client.js file... 这是client.js文件...

socket.on('gameScores', function(data) {
    if (data !== null) {
        if (JSON.parse(localStorage.reload))
            localStorage.reload = false;
        else
            localStorage.gameScores = JSON.stringify(data);
        localStorage.emptyScores = false;
    }
    else {
        localStorage.emptyScores = true;
        localStorage.reload = false;
    }
});
socket.on('defaultScores', function(data) {
    if (data == null) {
        console.log('Received Default from Server');
        localStorage.emptyScores = true;
        Game.main.gameScores('load');
    }
});

All the code of these two files is working correctly, except in the server.js file where you see the comment "Delete Success!" 除了在server.js文件中看到注释“ Delete Success!”之外,这两个文件的所有代码均正常工作。 that is followed by three lines of code. 然后是三行代码。 Of these three lines, the specific line which is not working is the following: 在这三行中,不起作用的特定行如下:

socket.emit('defaultScores', gameScores);

The above line of code should transmit a data packet to the corresponding socket receiver in the client.js file of label "defaultScores" but the client never receives it. 上面的代码行应将数据包发送到标签为“ defaultScores”的client.js文件中的相应套接字接收器,但客户端永远不会收到它。 I know the client does not receive it because the comment "Received Default from Server" does not print to client console. 我知道客户端不会收到它,因为注释“从服务器收到默认值”不会打印到客户端控制台。 Once the socket receiver in question is able to successfully work, thus printing the comment in question, the rest should work fine. 一旦有问题的套接字接收器能够成功工作,从而打印出有问题的注释,其余的就可以正常工作。

Thus, I am specifically requesting for assistance in getting the comment "Received Default from Server" to print to client console successfully. 因此,我特别要求获得帮助以使注释“从服务器接收默认值”成功打印到客户端控制台。

I managed to figure it out on my own. 我设法自己解决了。 Thankfully I came across the following previous post... 值得庆幸的是,我遇到了以下先前的帖子...

nodejs socket receive data from one port and make available on another port nodejs套接字从一个端口接收数据并在另一个端口上可用

From the article, I was able to infer that the change I needed, from the second line of code after the "Delete Success!" 从本文中,我可以从“删除成功!”之后的第二行代码中推断出我需要的更改。 comment, was to change to this... 评论,是要更改为...

ioMain.emit('defaultScores', gameScores);

This successfully resolved the problem. 这样就成功解决了问题。

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

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