Home > OS >  How can I use app.listen when the function is inside a var type variable?
How can I use app.listen when the function is inside a var type variable?

Time:11-18

I'm trying to make Autodesk forge web app as presenter web app & viewer web app for VR navigation. I followed the tutorial 'autodesk forge Share Viewer staste with websockets'. (And I learned javascript a little.)

Before, the last line was app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });

And the tutorial want me to adjust some additional lines to that line. code(it doesn't work) - start.js

const path = require('path');
const express = require('express');

const PORT = process.env.PORT || 3000;
const config = require('./config');
if (config.credentials.client_id == null || config.credentials.client_secret == null) {
    console.error('Missing FORGE_CLIENT_ID or FORGE_CLIENT_SECRET env. variables.');
    return;
}

let app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json({ limit: '50mb' }));
app.use('/api/forge/oauth', require('./routes/oauth'));
app.use('/api/forge/oss', require('./routes/oss'));
app.use('/api/forge/modelderivative', require('./routes/modelderivative'));
app.use((err, req, res, next) => {
    console.error(err);
    res.status(err.statusCode).json(err);
});

app.listen(PORT, () => { console.log(`Server listening on port ${PORT}`); });

var server = app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);

    var io = require('socket.io').listen(server);
    io.on('connection', function (socket) {
        // any custom action here?

        socket.on('disconnect', function () {
            // Any custom action?

        });

        socket.on('join', function (data) {
            socket.join(data.modelView);
        });

        socket.on('leave', function (data) {
            socket.leave(data.modelView);
        });

        socket.on('statechanged', function (data) {
            socket.to(data.modelView).emit('newstate', data.state);
        });
    });
});

my local folder, captured below. enter image description here

And this web site, https://forge.autodesk.com/blog/share-viewer-state-websockets

So how can I use app.listen when the app.listen function is inside a var type variable?

The problem part of above code.

var server = app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);

    var io = require('socket.io').listen(server);
    io.on('connection', function (socket) {
        // any custom action here?

        socket.on('disconnect', function () {
            // Any custom action?

        });

        socket.on('join', function (data) {
            socket.join(data.modelView);
        });

        socket.on('leave', function (data) {
            socket.leave(data.modelView);
        });

        socket.on('statechanged', function (data) {
            socket.to(data.modelView).emit('newstate', data.state);
        });
    });
});

I want the node.js run and duplicate one web app's camera view to another web app's camera view.

CodePudding user response:

Move the code that references the server variable and configures the io variable outside the callback like this:

const server = app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);
});

const io = require('socket.io')(server);
io.on('connection', function (socket) {
    // any custom action here?

    socket.on('disconnect', function () {
        // Any custom action?

    });

    socket.on('join', function (data) {
        socket.join(data.modelView);
    });

    socket.on('leave', function (data) {
        socket.leave(data.modelView);
    });

    socket.on('statechanged', function (data) {
        socket.to(data.modelView).emit('newstate', data.state);
    });
});

It is perfectly fine to set up the socket.io server before the .listen() has completed. The server object itself is already a proper server object that you can use as soon as app.listen() returns.

This then leaves the io variable in the outside scope where you can more easily use it from other code.

Note, using var is no longer preferred. Instead, use either let or const. I changed yours to const.

  • Related