javascript - nodejs cluster socket.io express app -
var httpsport = 8080, // used httpsapp httpport = 8000, // used httpapp numcpus = require('os').cpus().length; var credentials = { key: fs.readfilesync('./cert/client.key'), cert: fs.readfilesync('./cert/client.crt'), requestcert: true }; var cluster = require('cluster'), socketstore = require('socket.io-clusterhub'); var redis = require('redis'); var redisclient = redis.createclient(); if(cluster.ismaster) { for(var i=0; i<numcpus; i++) { cluster.fork(); } } else { var io = require('socket.io'), express = require('express'), httpsapp = express(), // https services httpapp = express(), // http services http = require('http'), https = require('https'); httpapp.configure( function () { httpapp.use(express.bodyparser()); httpapp.use(express.methodoverride); httpapp.use(httpapp.router); httpapp.set('port', httpport); httpapp.use(express.static(__dirname+'/public', {maxage:oneday})); }); httpsapp.configure( function() { // allow cors httpsapp.all('*', function(req, res, next){ if(!req.get('origin')) { return next(); } // use "*" here accept origin res.set('access-control-allow-origin', '*'); res.set('access-control-allow-methods', 'get'); res.set('access-control-allow-headers', 'x-requested-with, content-type'); // res.set('access-control-allow-max-age', 3600); if ('options' === req.method) { return res.send(200); } next(); }); httpsapp.use(express.bodyparser()); httpsapp.use(express.static(__dirname + '/public')); httpsapp.set('port', httpsport); }); var httpserver = http.createserver(httpapp), httpsserver = https.createserver(credentials, httpsapp), sio = io.listen(httpserver); // set configuration production. sio.configure('production', function(){ sio.enable('browser client etag'); sio.set('log level', 1); sio.set('store',socketstore); sio.set('transports', [ 'websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling' ]); }); sio.of('/namespace1').on('connection', function(socket) { socket.on('dosomething', function() { socket.emit('reply',{hello:world}) }); httpsapp.get("/user", function(req, res) { // ... }); } }
if node cluster used, i'm getting response: cannot /user. without cluster, able service https.get("/user").
also, using cluster, check if redis, http(s), socket.io , express module should declare in workers part or declare globally?
the httpsapp.get() nested within socket space because want reply specific socket. there way around structure?
anyone figure our why httpsapp.get() not servicing request? , declaration appropriate? thank you!
Comments
Post a Comment