Separating file server and socket.io logic in node.js

后端 未结 4 1222
栀梦
栀梦 2020-12-04 05:58

I\'m fairly new to node.js and I\'ve found its quite complicated separating a project into multiple files as the project grows in size. I had one large file before which ser

相关标签:
4条回答
  • 2020-12-04 06:24

    i would do something like this.

    app.js

    var app = require('http').createServer(handler),
        sockets = require('./sockets'),
        fs = require('fs');
    
    function handler (req, res) {
      fs.readFile(__dirname + '/index.html',
      function (err, data) {
        if (err) {
          res.writeHead(500);
          return res.end('Error loading index.html');
        }
    
        res.writeHead(200);
        res.end(data);
      });
    }
    
    sockets.startSocketServer(app);
    app.listen(80);
    

    and sockets.js

    var socketio = require('socket.io'),
            io, clients = {};
    
    module.exports = {
    
            startSocketServer: function (app) {
                    io = socketio.listen(app);
    
                    // configure
                    io.configure('development', function () {
                            //io.set('transports', ['websocket', 'xhr-polling']);
                            //io.enable('log');
                    });
    
                    io.configure('production', function () {
                            io.enable('browser client minification');  // send minified client
                            io.enable('browser client etag');          // apply etag caching logic based on version number
                            io.set('log level', 1);                    // reduce logging
                            io.set('transports', [                     // enable all transports (optional if you want flashsocket)
                                'websocket'
                              , 'flashsocket'
                              , 'htmlfile'
                              , 'xhr-polling'
                              , 'jsonp-polling'
                            ]);
                    });
                    //
    
                    io.sockets.on('connection', function (socket) {
                            console.log("new connection: " + socket.id);
    
                            socket.on('disconnect', function () {
                                    console.log("device disconnected");
    
                            });
    
                            socket.on('connect_device', function (data, fn) {
                                    console.log("data from connected device: " + data);
                                    for (var col in data) {
                                            console.log(col + " => " + data[col]);
                                    }
    
    
                            });
                    });
            }
    };
    

    i just copy&pasted some of my old code - don't really know what changed in the last versions of socket.io, but this is more about the structure than the actual code.

    and i would only use 2 files for your purposes, not 3. when you think about splitting it up further, maybe one other file for different routes ...

    hope this helps.

    0 讨论(0)
  • 2020-12-04 06:35

    I have had a crack at this as well and I am fairly happy with the result. Check out https://github.com/hackify/hackify-server for source code.

    0 讨论(0)
  • 2020-12-04 06:38

    I've another solution. You can use require.js creating a module and pass "app" as an argument. Within the module you can start socket.io and organize your sockets.

    app.js:

      var requirejs = require('requirejs');
    
      requirejs.config({
          baseUrl: './',
          nodeRequire: require
      });
    
      requirejs(['sockets'], function(sockets) {
    
        var app = require('http').createServer()
          , fs  = require('fs')
          , io  = sockets(app);
    
          // do something
          // add more sockets here using "io" resource
    
      });
    

    In your socket.js module you can do something like this:

      define(['socket.io'], function(socket){
        return function(app){
          var server = app.listen(3000) 
            , io     = socket.listen(server);
    
          io.sockets.on('connection', function (socket) {
            console.log('connected to socket');
    
            socket.emit('news', { hello: 'world' });
            socket.on('my other event', function (data) {
              console.log(data);
            });
    
            // more more more
    
          });
    
          return io;
        }
      });
    

    I hope help you with my contribution.

    0 讨论(0)
  • 2020-12-04 06:45

    In socket.io 0.8, you should attach events using io.sockets.on('...'), unless you're using namespaces, you seem to be missing the sockets part:

    io.listen(fileserver).sockets.on('connection', handler)
    

    It's probably better to avoid chaining it that way (you might want to use the io object later). The way I'm doing this right now:

    // sockets.js
    var socketio = require('socket.io')
    
    module.exports.listen = function(app){
        io = socketio.listen(app)
    
        users = io.of('/users')
        users.on('connection', function(socket){
            socket.on ...
        })
    
        return io
    }
    

    Then after creating the server app:

    // main.js
    var io = require('./lib/sockets').listen(app)
    
    0 讨论(0)
提交回复
热议问题