2

I have a running server with apache and Socket.IO. I'm trying to send and receive message using socket.io on my website.

This is the code of my server:

var fs = require('fs');
var hskey = fs.readFileSync('file.key');
var hscert = fs.readFileSync('file.crt');

var options = {
    key: hskey,
    cert: hscert
};

var app = require('https').createServer(options);
var io = require('/usr/local/lib/node_modules/socket.io').listen(app);

app.listen(8181);

io.sockets.on('connection', function (socket) {
  socket.emit('serverMessage', 'Bienvenue master!');
socket.broadcast.emit('serverMessage', 'New user online');
});

And this is the webpage:

<!doctype html>
<html>
  <head>
    <title>Socket.io Test</title>
    <script src="./socket.io.js"></script>
  </head>
  <body>

    <script>

    var socket;
    var firstconnect = true;

    function connect() {
      if(firstconnect) {
        socket = io.connect('https://secure.mysite.com:8181');

        socket.on('serverMessage', function(data){ message(data); });
        socket.on('connect', function(){ status_update("Connected to Server"); });
        socket.on('disconnect', function(){ status_update("Disconnected from Server"); });
        socket.on('reconnect', function(){ status_update("Reconnected to Server"); });
        socket.on('reconnecting', function( nextRetry ){ status_update("Reconnecting in " 
          + nextRetry + " seconds"); });
        socket.on('reconnect_failed', function(){ message("Reconnect Failed"); });

        firstconnect = false;
      }
      else {
        socket.socket.reconnect();
      }
    }

    function disconnect() {
      socket.disconnect();
    }

    function message(data) {
      document.getElementById('message').innerHTML += "<br>" + "Server says: " + data;
    }

    function status_update(txt){
      document.getElementById('status').innerHTML = txt;
    }

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

    function send() {
      socket.send('clientMessage', 'world');    
    };        

    </script>

    <h1>Socket.io Test</h1>
    <div><p id="status">Waiting for input</p></div>
    <div><p id="message"></p></div>  
    <button id="connect" onClick='connect()'/>Connect</button>
    <button id="disconnect" onClick='disconnect()'>Disconnect</button>
    <button id="send" onClick='send()'/>Send Message</button>
  </body>
</html>

Everything seems to work fine under Safari (websocket) and Opera (json pooling) but with Firefox and Chrome (websocket) I cannot send any message from the client to the server. Everything else is working, I can handshake, connect and gets server messages. I made allot of research but seems like I'm the only one with this problem.

Thanks for helping me!

6
  • Had a look at "Browser Compatibility" for websockets here? developer.mozilla.org/en/WebSockets Commented Apr 7, 2012 at 6:30
  • Chrome might also have a different websockets implementation than standard webkit -- really not sure on this. Commented Apr 7, 2012 at 6:32
  • I used socket.io for websocket because it support also flashsocket, and long pooling so it should work but sending messages dosen't Commented Apr 9, 2012 at 2:26
  • socket.io allows you to select which transport is used (client-side) with configuration parameters, so play around with those to see which ones are available/connecting. What versions of Firefox & Chrome are you using? The compatibility information on the socket.io website says that they should be well supported, even if not by websockets. Commented Apr 9, 2012 at 4:46
  • You must have server configuration issues I've been using Socket.IO with Chrome/Firefox/IE using web/flashsockets for some time now; with no issues. Commented Apr 9, 2012 at 13:20

3 Answers 3

2

I found the problem, I was using a different version of socket.io.js then the server side.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you please tell me how to check it. I'm also facing the same problem.
1

when you attach the socket.io module to express it intercepts the socket.io route.

So when you request "https://secure.mysite.com:8181/socket.io" it will respond with "Welcome to socket.io."

So when you request the client side socket.io.js it comes directly from the socket.io module. "https://secure.mysite.com:8181/socket.io/socket.io.js"

So If you wan't to mod the client side library you could create a modified copy and let express serve up the file, but as you update socketio through npm you'll have to bump up your modified copy as well manually.

Comments

0

if in FireFox you get this error - first check enabled or no proxy. and turnoff proxy if enabled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.