I have a website that is connected to NodeJS server via socket.io. When I 
open that website on different computer, only one or two of these devices 
would be able to send messages to the server. Help would be appreciated. I 
think there's something wrong with the way I implemented how the server 
handles package, but I can't figure out what.

*This is how I set up the socket.io part:*


var http = require('http')

  , net = require('net')

  , url = require('url')

  , fs = require('fs')

  , io = require('socket.io')

  , sys = require(process.binding('natives').util ? 'util' : 'sys')

  , server;

  

var tcpGuests = [];

var chatGuests = [];


// create http server with the website 

server = http.createServer(function(req, res){

  // your normal server code

  var path = url.parse(req.url).pathname;

  switch (path){

    case '/':

      res.writeHead(200, {'Content-Type': 'text/html'});

      res.write('<h1>Welcome. Try the <a href="/arduino.html">Arduino</a> 
example.</h1>');

      res.end();

      break;

      

    case '/json.js':

    case '/arduino.html':

      fs.readFile(__dirname + path, function(err, data){

        if (err) return send404(res);

        res.writeHead(200, {'Content-Type': path == 'json.js' ? 
'text/javascript' : 'text/html'})

        res.write(data, 'utf8');

        res.end();

      });

      break;

      

    default: send404(res);

  }

}),


send404 = function(res){

  res.writeHead(404);

  res.write('404');

  res.end();

};

//var app = connect().use(connect.static('public')).listen(8090, "0.0.0.0");

server.listen(8090, "0.0.0.0");

//server.listen(app);



// socket.io

var io = io.listen(server)

  , buffer = [];

  

io.on('connection', function(client){

  client.send({ buffer: buffer });

  client.broadcast.send({ announcement: client.sessionId + ' connected' });

  

  chatGuests.push(client);

  

  client.on('message', function(message){

    var msg = { message: [client.sessionId, message] };

    buffer.push(msg);

    if (buffer.length > 15) buffer.shift();

    client.broadcast.send(msg);

    

    //send msg to tcp connections

    for (g in tcpGuests) {

        tcpGuests[g].write(message);

    }

    

  });


  client.on('disconnect', function(){

    client.broadcast.send({ announcement: client.sessionId + ' 
disconnected' });


  });

});



*This is the base of the website (which has a button that sends a package 
to Node.js server):*


<!doctype html>

<html>

  <head>

    <title>socket.io client test</title>

        


    <!-- packet for socket io and JQuery-->    

    <script src="/json.js"></script> 

    <script src="/socket.io/socket.io.js"></script>

    <script 
src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js";></script>

    

  </head>

  


  <body>

    

    <script>

      // change button color and indicates who is pressing on button

      function message(obj){

        var el = document.createElement('p');

        if ('announcement' in obj) el.innerHTML = '<em>' + 
esc(obj.announcement) + '</em>';

        else if ('message' in obj) el.innerHTML = '<b>' + 
esc(obj.message[0]) + ':</b> ' + esc(obj.message[1]);

        

        if (obj.message[0] == "you" && obj.message[1] == "1") {

            jQuery('#button').css('background-color','green');

            

        } else if (obj.message[0] != "arduino" && obj.message[0] != "you" 
&& obj.message[1] == "1") {

            jQuery('#button').css('background-color','yellow');

            

        } else if (obj.message[1] == "0") {

            jQuery('#button').css('background-color','white');            

        }

        

        if( obj.message && window.console && console.log ) 
console.log(obj.message[0], obj.message[1]);

        

      }


      function turnOn() {

          socket.send('1');

          message({ message: ['you', '1'] });

      }

      

      function turnOff() {

          socket.send('0');

          message({ message: ['you', '0'] });

      }

      

      function esc(msg){

        return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');

      };

      

*      // establish socket connection via port 8090*

*      //var socket = new io.connect(null, { port: 8090, rememberTransport: 
false});*

*      var socket = io.connect('98.174.142.4:8090');*



*      socket.on('message', function(obj){*

*        if ('buffer' in obj){*

*          //ignore this*

*        } else message(obj);*

*      });*

      

*      socket.on('connect', function(){ message({ message: ['System', 
'Connected']})});*

*      socket.on('disconnect', function(){ message({ message: ['System', 
'Disconnected']})});*

*      socket.on('reconnect', function(){ message({ message: ['System', 
'Reconnected to server']})});*

*      socket.on('reconnecting', function( nextRetry ){ message({ message: 
['System', 'Attempting to re-connect to the server, next attempt in ' + 
nextRetry + 'ms']})});*

*      socket.on('reconnect_failed', function(){ message({ message: 
['System', 'Reconnected to server FAILED.']})});*

      


      var logged = false;

      // when mouse down; turn on.

      jQuery('document').ready(function(){

          jQuery('#button').mousedown(function() {

        logged = !logged;

if( logged == true)      

{

  turnOn();

}else

{

          turnOff();

}

          });

                  

      }) 

    </script>

    

    <div id='button'>

        <br/><Br/>

        Tap to turn LED my on.

        <br/><br/>

        Release to turn LED off.


        <p>

            <span id="greenLegend">Green</span> => You are blinking it.<br/>

            <span id="yellowLegend">Yellow</span> => Someone else is 
blinking it

        <p>


    </div>

    

    

    

    <style>

        body { 

            width:960px;

            height:320px;

        }

        #button {

            border:2px solid black;

            padding:12px;

            text-align:center;

            font-family:'Helvetica';

            font-size:30px;

            margin:0 auto;

            width:90%;

            height:90%;


        }

        p { font-size:18px;}

        #greenLegend { color:#3EA055; font-size:18px;}

        #yellowLegend { color:#CDAD00; font-size:18px;}


    </style>

    

  </body>

</html>


-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/18bd0a9c-8c0f-480b-bacf-54196a61fdfc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to