Monday, 15 April 2013

javascript - Hosting a nodejs server on dotcloud -



javascript - Hosting a nodejs server on dotcloud -

i trying host nodejs app on hosting service "dotcloud". nodejs uses bundle "websocket" handle communications. ie. npm install websocket

my app works great when it's running on localhost on laptop. when deploy app on dotcloud not work correctly.

here going on: point browser url on dotcloud: pirate-captainlonate.dotcloud.com

then, express handles request express.get('/'.......){} express serves .html page client expect. .html file in turn tries found websocket connection server. 1 time again can work fine on local machine. however, no connection beingness established. specifically, dotcloud serving me .html file, .html file not establishing websocket connection server. connection.onerror isn't beingness called either. it's weird.

here code help understand i'm doing:

client side:

this.connection = new websocket('ws://pirate-captainlonate.dotcloud.com:1337'); this.connection.onerror = function (error) { console.log("error connection *sadface*"); }; **** note note onerror function here show indeed have set up, it's not beingness called. seem no error beingness thrown.

server side:

var websocketserver = require('websocket').server; // websocket var server = require('http').createserver(); var expr = require("express"); // load express module var xpress = expr(); // xpress holds server object // helps node serve game.html page upon request xpress.configure(function() { xpress.use(expr.static(__dirname + "/public")); xpress.set("view options", {layout: false}); }); // requests root serve game.html page xpress.get('/', function(req, res) { res.sendfile(__dirname + '/public/game.html'); }); // ports hear on var websocketsserverport = 1337; xpress.listen(8080); server.listen(websocketsserverport, function() { console.log((new date()) + " server listening on port " + websocketsserverport); }); // websocket server var wsserver = new websocketserver({ httpserver: server });

that should plenty code show guys how it's working. 1 of ask, ">> dotcloud logs" showing?

[www.0] ==> /var/log/supervisor/app.log <== [www.0] sat feb 16 2013 02:57:59 gmt+0000 (utc) server listening on port 1337 [www.0] ==> /var/log/supervisor/supervisord.log <== [www.0] 2013-02-16 02:57:57,946 warn included file "/home/dotcloud/current/supervisord.conf" during parsing [www.0] 2013-02-16 02:57:58,033 info rpc interface 'supervisor' initialized [www.0] 2013-02-16 02:57:58,033 warn celementtree not installed, using slower xml parser xml-rpc [www.0] 2013-02-16 02:57:58,033 crit server 'unix_http_server' running without http authentication checking [www.0] 2013-02-16 02:57:58,038 info daemonizing supervisord process [www.0] 2013-02-16 02:57:58,039 info supervisord started pid 140 [www.0] 2013-02-16 02:57:59,048 info spawned: 'app' pid 154 [www.0] 2013-02-16 02:58:00,290 info success: app entered running state, process has stayed > 1 seconds (startsecs) [db.0] ==> /var/log/mongodb/mongodb.log <== [db.0] sat feb 16 01:45:02 [conn4] end connection 127.0.0.1:51326 (0 connections open)

alright, i'd working. i've been @ forever. allow me know if there else guys need help me reply question.

thanks,

--nathan

addendum: how server sending html file.

xpress.get('/', function(req, res) { res.sendfile(__dirname + '/public/game.html'); });

it looks trying utilize 2 http ports service, , dotcloud supports 1 out of box, need allow them know want have 1 adding little snippet in dotcloud.yml

here illustration dotcloud.yml asking sec tcp port called server

app: type: nodejs ports: server: tcp config: node_version: v0.8.x

once add together , push, server given 2nd tcp port can utilize server, need find out port getting value environment file.

here snippet port env, default 4242 when not there can still run locally.

var websocketsserverport = process.env['port_server'] || 4242;

if wondering how got env variable name, simple. port_ , uppercase string of name dotcloud.yml. since used server above became port_server, if used node have been port_node, set want, create sure values match.

client:

to find out port need connect on client, need go environment variables again. time looking variable looks dotcloud_app_server_port. important: your variable name might different

how did envirornment variable name?

the name of variable looks dotcloud_{{app_name}}_{{port_name}}_port uppercase. replace {{variable}}'s info below.

{{app_name}} = name of app dotcloud.yml, in illustration above app {{port_name}} = port name, server in dotcloud.yml illustration above.

to find can apps environment.json, environment.yml files, shell env variables, or log dotcloud dashboard, click on app, , environment tab see list of application variables.

if create 3 changes, problems should go away.

if need more code examples, please check out github repo doing similar trying do.

https://github.com/3on/node-tcp-on-dotcloud

javascript node.js websocket dotcloud

No comments:

Post a Comment