Sails.jsをリバースプロキシ経由で実行する(Sails.ioと接続出来ない)

node モジュール node-http-proxy を使い、リバースプロキシ経由でsails.jsを使っている環境があるのですが、Sails.io(Socket.io)への接続がうまくいかずどう対応するのか悩みましたが、実行しているポートへ直接呼び出すことで接続できました。

Version: Sails.js@v0.9.6

/assets(/linker)/js/app.js

(function (io) {

  // as soon as this file is loaded, connect automatically,
  //var socket = io.connect();
  var socket = io.connect(“/hogehoge", {port: 起動ポート});

view(layout.ejs) 側で起動ポートを取得します。

/views/layout.ejs

<%
  // for reverse-proxy
  var tag = "<script/>window.sailsPort = " + Hook.config.port + "</script/>" %>
<%- tag %> 

あと、リバースプロキシとは直接関係ないですが io.connect を色んなページで取り回したいので window.Socketer を設定しています。

# Socketer.coffee
do(w=window,d=document) ->
 
  # ref http://qiita.com/Layzie/items/465e715dae14e2f601de
  _is = (type, obj) ->
    clas = Object.prototype.toString.call(obj).slice(8, -1)
    return obj isnt undefined && obj isnt null && clas is type
 
  # log output
  log = ->
    if (typeof console isnt 'undefined')
      console.log.apply(console, arguments);
 
  authority = ->
    href = location.href.match(/^(https?):\/{2,3}(([0-9a-zA-Z\.\-:]+?):?([0-9]*?))\//i)
    return {
      input: href[0]
      url: href[2]
      protcol: href[1]
      fqdn: href[3]
      port: href[4]
    }
 
  domain = ->
    ref = authority()
    base = ref.protcol + "://" + ref.fqdn + ":" + ref.port
    if( base != ref.url and ref.port == "" )
      port = if ref.protcol == "https" then "433" else "80"
      base = base + port
 
    return base
 
  ## WebSocket room implements
  class Socketer
 
    constructor: ( @io, details ) ->
 
      @self = this
      @details = details
      @domain = domain()
      @volatile= new Array()
      return
 
    getSocket: ( room ) ->
 
      if ( ! _is( "String", room ) )
        room = ""
 
      sockets = @io.sockets[@domain]
      return if sockets then sockets.namespaces[room] else undefined
 
    bind: ( room="", connect=(->), disconnect=(->) ) ->
 
      if ( _is( "Function", room ) )
        disconnect = connect
        connect = room
        room = ""
 
      if ( ! _is( "String", room ) )
        room = ""
 
      socket = @self.getSocket(room)
 
      if( socket )
        log( "Already Connecting...", socket )
        if( socket.socket.isClosed )
          socket.socket.open()
        socket.socket.connect(connect)
        return @self
 
      if room == ""
        socket = @io.connect( undefined, @self.details )
      else
        socket = @io.connect( room, @self.details )
 
      socket.on "connect", ->
 
        if( _is("Function",connect) )
          # callback
          setTimeout( ->
            connect( io.sockets, socket )
          ,1)
 
      socket.on "disconnect", ->
        if( _is("Function", disconnect) )
          setTimeout( ->
            disconnect( io.sockets, socket )
          ,1)
 
      return @self
 
    unbind: (room="") ->
 
      socket = @self.getSocket(room)
      if( socket )
        socket.disconnect()
      return @self
 
  ## end class Socketer
 
  instance = null
  w.Socketer = (io, details) ->
    if ( instance )
      instance
    else
      instance = new Socketer(io, details)