node.js - var socket = io.connect('http://yourhostname/');? -


i try socket.io again since v.1.0 released.

as doc, https://github.com/automattic/socket.io


server side:

var server = require('http').server(); var io = require('socket.io')(server);  io.on('connection', function(socket){   socket.on('event', function(data){});   socket.on('disconnect', function(){}); }); server.listen(5000); 

client side

var socket = io.connect('http://yourhostname.com/');

in development, surely var socket = io.connect('http://localhost:5000/');


it works, i'm uncomfortable hardcoding hostname(subdomain.domain) in client code(/index.js).

the index.js hosted http-sever , socket.io bundled http-server in configuration.

is there smart way not hardcode hostname code in relative path?

thanks.

edit:

when try:

var socket = io.connect('./'); 

the connection error:

get http://.:5000/socket.io/?eio=2&transport=polling&t=1401659441615-0 net::err_name_not_resolved 

is this, @ least port number (5000) obtained without hardcoding in client side.

final answer.

i have totally forgotton can obtain current url/domain in browser.

window.location.hostname

so, goes:

'use strict'; /*global window, require, console, __dirname, $,alert*/  var log = function(msg) {     console.log(msg); }; log('init');  $('document').ready(function() {     var io = require('socket.io-client');      var socket = io.connect(window.location.hostname);     socket.on('connect', function()     {         log('socket connected');     });  }); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -