Frage

I'm doing a realtime app with geddy framework (the basic chat example). But I get and error when the client tries to establish the connection.

here's the server-side code (on the init.js file):

var io = require('socket.io').listen(geddy.server);
io.sockets.on('connection', function (socket) {
  console.log("Good!");
  socket.emit('new', { message: 'world' });
  socket.on('newMessage', function (data) {
    console.log(data);
  });
});

and the client-side code:

$(document).ready(function(){
    startSockets();
});

function startSockets(){
  var socket = io.connect('http://localhost:4004');
  socket.on('new', function (data) {
    alert(data);
    //socket.emit('newMessage', { my: 'data' });
  });
}

When I try to connect to localhost:4004/ I get the next warn:

   debug - setting request GET /socket.io/1/websocket/G_GapksVv1J4iBZIUVe3
   debug - set heartbeat interval for client G_GapksVv1J4iBZIUVe3
   debug - websocket writing 7:::1+0
   warn  - client not handshaken client should reconnect
   info  - transport end (error)
   debug - set close timeout for client G_GapksVv1J4iBZIUVe3
   debug - cleared close timeout for client G_GapksVv1J4iBZIUVe3
   debug - cleared heartbeat interval for client G_GapksVv1J4iBZIUVe3
   debug - discarding transport

besides Chrome console gives this error:

WebSocket is closed before the connection is established. 

I don't know what can cause these. Any ideas?

War es hilfreich?

Lösung

The problem is that Socket.io needs to connect to the server after it's started, and the server doesn't start until after the code in init.js has run. The current (hacky) solution in the existing built-in RT code (as of Geddy v0.11) is to put this sort of code in an after_start.js file in the config directory, which Geddy runs after the server starts up. This should work as a workaround in this case too, where you're wiring up Socket.io to the server yourself.

This is obviously not ideal, and a major goal for v0.12 is fixing up the RT integration so it's much more usable and useful. If you have input into how you think this should look, definitely hit us up in IRC (#geddy on Freenode.net), or on the mailing list (https://groups.google.com/group/geddyjs).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top