简体   繁体   中英

Node.js Emit Packet across Ports

I have a server.js and client.js pair of script files.

Here is the server.js file...

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...

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!" 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. 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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